liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/*!
******************************************************************************
* \file mb_model_based.c
*
* \brief
*    This file contain mb level API functions
*
* \date
*
* \author
*    ittiam
*
******************************************************************************
*/
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* User include files */
#include "ittiam_datatypes.h"
#include "rc_common.h"
#include "rc_cntrl_param.h"
#include "var_q_operator.h"
#include "mem_req_and_acq.h"
#include "mb_model_based.h"
 
typedef struct mb_rate_control_t
{
    /* Frame Qp */
    UWORD8 u1_frm_qp;
    /* Estimated average activity for the current frame (updated with the previous
    frame activity since it is independent of picture type whether it is I or P) */
    WORD32 i4_avg_activity;
} mb_rate_control_t;
 
WORD32 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)
{
    WORD32 i4_mem_tab_idx = 0;
    static mb_rate_control_t s_mb_rate_control_temp;
 
    /* Hack for al alloc, during which we dont have any state memory.
      Dereferencing can cause issues */
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
        (*pps_mb_rate_control) = &s_mb_rate_control_temp;
 
    /*for src rate control state structure*/
    if(e_func_type != GET_NUM_MEMTAB)
    {
        fill_memtab(
            &ps_memtab[i4_mem_tab_idx],
            sizeof(mb_rate_control_t),
            MEM_TAB_ALIGNMENT,
            PERSISTENT,
            DDR);
        use_or_fill_base(&ps_memtab[0], (void **)pps_mb_rate_control, e_func_type);
    }
    i4_mem_tab_idx++;
 
    return (i4_mem_tab_idx);
}
 
/********************************************************************************
                         MB LEVEL API FUNCTIONS
********************************************************************************/
/******************************************************************************
  Function Name   : init_mb_level_rc
  Description     : Initialise the mb model and the average activity to default values
  Arguments       :
  Return Values   : void
  Revision History:
                    13 03 2008   KJN  Creation
*********************************************************************************/
void init_mb_level_rc(mb_rate_control_t *ps_mb_rate_control)
{
    /* Set values to default */
    ps_mb_rate_control->i4_avg_activity = 0;
}
/******************************************************************************
  Function Name   : mb_init_frame_level
  Description     : Initialise the mb state with frame level decisions
  Arguments       : u1_frame_qp - Frame level qp
  Return Values   : void
  Revision History:
                    13 03 2008   KJN  Creation
*********************************************************************************/
void mb_init_frame_level(mb_rate_control_t *ps_mb_rate_control, UWORD8 u1_frame_qp)
{
    /* Update frame level QP */
    ps_mb_rate_control->u1_frm_qp = u1_frame_qp;
}
/******************************************************************************
  Function Name   : reset_mb_activity
  Description     : Reset the mb activity - Whenever there is SCD
                    the mb activity is reset
  Arguments       :
  Return Values   : void
  Revision History:
                    13 03 2008   KJN  Creation
*********************************************************************************/
void reset_mb_activity(mb_rate_control_t *ps_mb_rate_control)
{
    ps_mb_rate_control->i4_avg_activity = 0;
}
 
/******************************************************************************
  Function Name   : get_mb_qp
  Description     : Calculates the mb level qp
  Arguments       : i4_cur_mb_activity - current frame mb activity
                    pi4_mb_qp - Array of 2 values for before and after mb activity
                                modulation
  Return Values   : void
 
  Revision History:
                    13 03 2008   KJN  Creation
*********************************************************************************/
void get_mb_qp(mb_rate_control_t *ps_mb_rate_control, WORD32 i4_cur_mb_activity, WORD32 *pi4_mb_qp)
{
    WORD32 i4_qp;
    /* Initialise the mb level qp with the frame level qp */
    i4_qp = ps_mb_rate_control->u1_frm_qp;
 
    /* Store the model based QP - This is used for updating the rate control model */
    pi4_mb_qp[0] = i4_qp;
 
    /* Modulate the Qp based on the activity */
    if((ps_mb_rate_control->i4_avg_activity) && (i4_qp < 100))
    {
        i4_qp = ((((2 * i4_cur_mb_activity)) + ps_mb_rate_control->i4_avg_activity) * i4_qp +
                 ((i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity) >> 1)) /
                (i4_cur_mb_activity + 2 * ps_mb_rate_control->i4_avg_activity);
 
        if(i4_qp > ((3 * ps_mb_rate_control->u1_frm_qp) >> 1))
            i4_qp = ((3 * ps_mb_rate_control->u1_frm_qp) >> 1);
    }
 
    /* Store the qp modulated by mb activity - This is used for encoding the MB */
    pi4_mb_qp[1] = i4_qp;
}
/******************************************************************************
  Function Name   : get_frm_level_qp
  Description     : Returns the stored frame level QP
  Arguments       :
  Revision History:
                    13 03 2008   KJN  Creation
*********************************************************************************/
UWORD8 get_frm_level_qp(mb_rate_control_t *ps_mb_rate_control)
{
    return (ps_mb_rate_control->u1_frm_qp);
}
/******************************************************************************
  Function Name   : mb_update_frame_level
  Description     : Update the frame level info collected
  Arguments       : i4_avg_activity - Average activity fot frame
  Return Values   :
  Revision History:
                    13 03 2008   KJN  Creation
*********************************************************************************/
void mb_update_frame_level(mb_rate_control_t *ps_mb_rate_control, WORD32 i4_avg_activity)
{
    /*****************************************************************************
                    Update the Average Activity
    *****************************************************************************/
    ps_mb_rate_control->i4_avg_activity = i4_avg_activity;
}