summaryrefslogtreecommitdiffstats
path: root/encoder/irc_mb_model_based.c
blob: b8e3d1b8854bb185c28e722ca96599966a76a1b2 (plain)
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
/******************************************************************************
 *
 * Copyright (C) 2015 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 Includes                                                             */
/*****************************************************************************/

/* User include files */
#include "irc_datatypes.h"
#include "irc_cntrl_param.h"
#include "irc_mem_req_and_acq.h"
#include "irc_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 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)
{
    WORD32 i4_mem_tab_idx = 0;
    mb_rate_control_t s_mb_rate_control_temp;

    /*
     * Hack for al alloc, during which we don't 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),
                    ALIGN_128_BYTE, 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
 ******************************************************************************/

/******************************************************************************
 Description     : Initialize the mb model and the average activity to default
                   values
 ******************************************************************************/
void irc_init_mb_level_rc(mb_rate_control_t *ps_mb_rate_control)
{
    /* Set values to default */
    ps_mb_rate_control->i4_avg_activity = 0;
}

/******************************************************************************
 Description     : Initialize the mb state with frame level decisions
 *********************************************************************************/
void irc_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;
}

/******************************************************************************
 Description     : Reset the mb activity - Whenever there is SCD
                   the mb activity is reset
 *********************************************************************************/
void irc_reset_mb_activity(mb_rate_control_t *ps_mb_rate_control)
{
    ps_mb_rate_control->i4_avg_activity = 0;
}

/******************************************************************************
 Description     : Calculates the mb level qp
 *********************************************************************************/
void irc_get_mb_qp(mb_rate_control_t *ps_mb_rate_control,
                   WORD32 i4_cur_mb_activity,
                   WORD32 *pi4_mb_qp)
{
    WORD32 i4_qp;
    /* Initialize 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;
}

/*******************************************************************************
 Description     : Returns the stored frame level QP
 ******************************************************************************/
UWORD8 irc_get_frm_level_qp(mb_rate_control_t *ps_mb_rate_control)
{
    return (ps_mb_rate_control->u1_frm_qp);
}

/*******************************************************************************
 Description     : Update the frame level info collected
 ******************************************************************************/
void irc_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;
}