xref: /aosp_15_r20/external/libavc/encoder/irc_mb_model_based.c (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1*495ae853SAndroid Build Coastguard Worker /******************************************************************************
2*495ae853SAndroid Build Coastguard Worker  *
3*495ae853SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
4*495ae853SAndroid Build Coastguard Worker  *
5*495ae853SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*495ae853SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*495ae853SAndroid Build Coastguard Worker  * You may obtain a copy of the License at:
8*495ae853SAndroid Build Coastguard Worker  *
9*495ae853SAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
10*495ae853SAndroid Build Coastguard Worker  *
11*495ae853SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*495ae853SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*495ae853SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*495ae853SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*495ae853SAndroid Build Coastguard Worker  * limitations under the License.
16*495ae853SAndroid Build Coastguard Worker  *
17*495ae853SAndroid Build Coastguard Worker  *****************************************************************************
18*495ae853SAndroid Build Coastguard Worker  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19*495ae853SAndroid Build Coastguard Worker */
20*495ae853SAndroid Build Coastguard Worker 
21*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
22*495ae853SAndroid Build Coastguard Worker /* File Includes                                                             */
23*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
24*495ae853SAndroid Build Coastguard Worker 
25*495ae853SAndroid Build Coastguard Worker /* User include files */
26*495ae853SAndroid Build Coastguard Worker #include "irc_datatypes.h"
27*495ae853SAndroid Build Coastguard Worker #include "irc_cntrl_param.h"
28*495ae853SAndroid Build Coastguard Worker #include "irc_mem_req_and_acq.h"
29*495ae853SAndroid Build Coastguard Worker #include "irc_mb_model_based.h"
30*495ae853SAndroid Build Coastguard Worker 
31*495ae853SAndroid Build Coastguard Worker typedef struct mb_rate_control_t
32*495ae853SAndroid Build Coastguard Worker {
33*495ae853SAndroid Build Coastguard Worker     /* Frame Qp */
34*495ae853SAndroid Build Coastguard Worker     UWORD8 u1_frm_qp;
35*495ae853SAndroid Build Coastguard Worker 
36*495ae853SAndroid Build Coastguard Worker     /*
37*495ae853SAndroid Build Coastguard Worker      * Estimated average activity for the current frame (updated with the
38*495ae853SAndroid Build Coastguard Worker      * previous frame activity since it is independent of picture type whether
39*495ae853SAndroid Build Coastguard Worker      * it is I or P)
40*495ae853SAndroid Build Coastguard Worker      */
41*495ae853SAndroid Build Coastguard Worker     WORD32 i4_avg_activity;
42*495ae853SAndroid Build Coastguard Worker 
43*495ae853SAndroid Build Coastguard Worker } mb_rate_control_t;
44*495ae853SAndroid Build Coastguard Worker 
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*495ae853SAndroid Build Coastguard Worker WORD32 irc_mbrc_num_fill_use_free_memtab(mb_rate_control_t **pps_mb_rate_control,
46*495ae853SAndroid Build Coastguard Worker                                          itt_memtab_t *ps_memtab,
47*495ae853SAndroid Build Coastguard Worker                                          ITT_FUNC_TYPE_E e_func_type)
48*495ae853SAndroid Build Coastguard Worker {
49*495ae853SAndroid Build Coastguard Worker     WORD32 i4_mem_tab_idx = 0;
50*495ae853SAndroid Build Coastguard Worker     mb_rate_control_t s_mb_rate_control_temp;
51*495ae853SAndroid Build Coastguard Worker 
52*495ae853SAndroid Build Coastguard Worker     /*
53*495ae853SAndroid Build Coastguard Worker      * Hack for al alloc, during which we don't have any state memory.
54*495ae853SAndroid Build Coastguard Worker      * Dereferencing can cause issues
55*495ae853SAndroid Build Coastguard Worker      */
56*495ae853SAndroid Build Coastguard Worker     if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
57*495ae853SAndroid Build Coastguard Worker     {
58*495ae853SAndroid Build Coastguard Worker         (*pps_mb_rate_control) = &s_mb_rate_control_temp;
59*495ae853SAndroid Build Coastguard Worker     }
60*495ae853SAndroid Build Coastguard Worker 
61*495ae853SAndroid Build Coastguard Worker     /*For src rate control state structure*/
62*495ae853SAndroid Build Coastguard Worker     if(e_func_type != GET_NUM_MEMTAB)
63*495ae853SAndroid Build Coastguard Worker     {
64*495ae853SAndroid Build Coastguard Worker         fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(mb_rate_control_t),
65*495ae853SAndroid Build Coastguard Worker                     ALIGN_128_BYTE, PERSISTENT, DDR);
66*495ae853SAndroid Build Coastguard Worker         use_or_fill_base(&ps_memtab[0], (void**)pps_mb_rate_control,
67*495ae853SAndroid Build Coastguard Worker                          e_func_type);
68*495ae853SAndroid Build Coastguard Worker     }
69*495ae853SAndroid Build Coastguard Worker     i4_mem_tab_idx++;
70*495ae853SAndroid Build Coastguard Worker 
71*495ae853SAndroid Build Coastguard Worker     return (i4_mem_tab_idx);
72*495ae853SAndroid Build Coastguard Worker }
73*495ae853SAndroid Build Coastguard Worker 
74*495ae853SAndroid Build Coastguard Worker /*******************************************************************************
75*495ae853SAndroid Build Coastguard Worker  MB LEVEL API FUNCTIONS
76*495ae853SAndroid Build Coastguard Worker  ******************************************************************************/
77*495ae853SAndroid Build Coastguard Worker 
78*495ae853SAndroid Build Coastguard Worker /******************************************************************************
79*495ae853SAndroid Build Coastguard Worker  Description     : Initialize the mb model and the average activity to default
80*495ae853SAndroid Build Coastguard Worker                    values
81*495ae853SAndroid Build Coastguard Worker  ******************************************************************************/
irc_init_mb_level_rc(mb_rate_control_t * ps_mb_rate_control)82*495ae853SAndroid Build Coastguard Worker void irc_init_mb_level_rc(mb_rate_control_t *ps_mb_rate_control)
83*495ae853SAndroid Build Coastguard Worker {
84*495ae853SAndroid Build Coastguard Worker     /* Set values to default */
85*495ae853SAndroid Build Coastguard Worker     ps_mb_rate_control->i4_avg_activity = 0;
86*495ae853SAndroid Build Coastguard Worker }
87*495ae853SAndroid Build Coastguard Worker 
88*495ae853SAndroid Build Coastguard Worker /******************************************************************************
89*495ae853SAndroid Build Coastguard Worker  Description     : Initialize the mb state with frame level decisions
90*495ae853SAndroid Build Coastguard Worker  *********************************************************************************/
irc_mb_init_frame_level(mb_rate_control_t * ps_mb_rate_control,UWORD8 u1_frame_qp)91*495ae853SAndroid Build Coastguard Worker void irc_mb_init_frame_level(mb_rate_control_t *ps_mb_rate_control,
92*495ae853SAndroid Build Coastguard Worker                              UWORD8 u1_frame_qp)
93*495ae853SAndroid Build Coastguard Worker {
94*495ae853SAndroid Build Coastguard Worker     /* Update frame level QP */
95*495ae853SAndroid Build Coastguard Worker     ps_mb_rate_control->u1_frm_qp = u1_frame_qp;
96*495ae853SAndroid Build Coastguard Worker }
97*495ae853SAndroid Build Coastguard Worker 
98*495ae853SAndroid Build Coastguard Worker /******************************************************************************
99*495ae853SAndroid Build Coastguard Worker  Description     : Reset the mb activity - Whenever there is SCD
100*495ae853SAndroid Build Coastguard Worker                    the mb activity is reset
101*495ae853SAndroid Build Coastguard Worker  *********************************************************************************/
irc_reset_mb_activity(mb_rate_control_t * ps_mb_rate_control)102*495ae853SAndroid Build Coastguard Worker void irc_reset_mb_activity(mb_rate_control_t *ps_mb_rate_control)
103*495ae853SAndroid Build Coastguard Worker {
104*495ae853SAndroid Build Coastguard Worker     ps_mb_rate_control->i4_avg_activity = 0;
105*495ae853SAndroid Build Coastguard Worker }
106*495ae853SAndroid Build Coastguard Worker 
107*495ae853SAndroid Build Coastguard Worker /******************************************************************************
108*495ae853SAndroid Build Coastguard Worker  Description     : Calculates the mb level qp
109*495ae853SAndroid Build Coastguard Worker  *********************************************************************************/
irc_get_mb_qp(mb_rate_control_t * ps_mb_rate_control,WORD32 i4_cur_mb_activity,WORD32 * pi4_mb_qp)110*495ae853SAndroid Build Coastguard Worker void irc_get_mb_qp(mb_rate_control_t *ps_mb_rate_control,
111*495ae853SAndroid Build Coastguard Worker                    WORD32 i4_cur_mb_activity,
112*495ae853SAndroid Build Coastguard Worker                    WORD32 *pi4_mb_qp)
113*495ae853SAndroid Build Coastguard Worker {
114*495ae853SAndroid Build Coastguard Worker     WORD32 i4_qp;
115*495ae853SAndroid Build Coastguard Worker     /* Initialize the mb level qp with the frame level qp */
116*495ae853SAndroid Build Coastguard Worker     i4_qp = ps_mb_rate_control->u1_frm_qp;
117*495ae853SAndroid Build Coastguard Worker 
118*495ae853SAndroid Build Coastguard Worker     /*
119*495ae853SAndroid Build Coastguard Worker      * Store the model based QP - This is used for updating the rate control model
120*495ae853SAndroid Build Coastguard Worker      */
121*495ae853SAndroid Build Coastguard Worker     pi4_mb_qp[0] = i4_qp;
122*495ae853SAndroid Build Coastguard Worker 
123*495ae853SAndroid Build Coastguard Worker     /* Modulate the Qp based on the activity */
124*495ae853SAndroid Build Coastguard Worker     if((ps_mb_rate_control->i4_avg_activity) && (i4_qp < 100))
125*495ae853SAndroid Build Coastguard Worker     {
126*495ae853SAndroid Build Coastguard Worker         i4_qp =((((2 * i4_cur_mb_activity))
127*495ae853SAndroid Build Coastguard Worker                + ps_mb_rate_control->i4_avg_activity)* i4_qp
128*495ae853SAndroid Build Coastguard Worker                + ((i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity)
129*495ae853SAndroid Build Coastguard Worker                >> 1))/ (i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity);
130*495ae853SAndroid Build Coastguard Worker 
131*495ae853SAndroid Build Coastguard Worker         if(i4_qp > ((3 * ps_mb_rate_control->u1_frm_qp) >> 1))
132*495ae853SAndroid Build Coastguard Worker         {
133*495ae853SAndroid Build Coastguard Worker             i4_qp = ((3 * ps_mb_rate_control->u1_frm_qp) >> 1);
134*495ae853SAndroid Build Coastguard Worker         }
135*495ae853SAndroid Build Coastguard Worker     }
136*495ae853SAndroid Build Coastguard Worker 
137*495ae853SAndroid Build Coastguard Worker     /* Store the qp modulated by mb activity - This is used for encoding the MB */
138*495ae853SAndroid Build Coastguard Worker     pi4_mb_qp[1] = i4_qp;
139*495ae853SAndroid Build Coastguard Worker }
140*495ae853SAndroid Build Coastguard Worker 
141*495ae853SAndroid Build Coastguard Worker /*******************************************************************************
142*495ae853SAndroid Build Coastguard Worker  Description     : Returns the stored frame level QP
143*495ae853SAndroid Build Coastguard Worker  ******************************************************************************/
irc_get_frm_level_qp(mb_rate_control_t * ps_mb_rate_control)144*495ae853SAndroid Build Coastguard Worker UWORD8 irc_get_frm_level_qp(mb_rate_control_t *ps_mb_rate_control)
145*495ae853SAndroid Build Coastguard Worker {
146*495ae853SAndroid Build Coastguard Worker     return (ps_mb_rate_control->u1_frm_qp);
147*495ae853SAndroid Build Coastguard Worker }
148*495ae853SAndroid Build Coastguard Worker 
149*495ae853SAndroid Build Coastguard Worker /*******************************************************************************
150*495ae853SAndroid Build Coastguard Worker  Description     : Update the frame level info collected
151*495ae853SAndroid Build Coastguard Worker  ******************************************************************************/
irc_mb_update_frame_level(mb_rate_control_t * ps_mb_rate_control,WORD32 i4_avg_activity)152*495ae853SAndroid Build Coastguard Worker void irc_mb_update_frame_level(mb_rate_control_t *ps_mb_rate_control,
153*495ae853SAndroid Build Coastguard Worker                                WORD32 i4_avg_activity)
154*495ae853SAndroid Build Coastguard Worker {
155*495ae853SAndroid Build Coastguard Worker      /* Update the Average Activity */
156*495ae853SAndroid Build Coastguard Worker      ps_mb_rate_control->i4_avg_activity = i4_avg_activity;
157*495ae853SAndroid Build Coastguard Worker }
158