18d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/******************************************************************************
28d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
38d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * Copyright (C) 2015 The Android Open Source Project
48d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
58d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * Licensed under the Apache License, Version 2.0 (the "License");
68d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * you may not use this file except in compliance with the License.
78d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * You may obtain a copy of the License at:
88d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
98d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * http://www.apache.org/licenses/LICENSE-2.0
108d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
118d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * Unless required by applicable law or agreed to in writing, software
128d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * distributed under the License is distributed on an "AS IS" BASIS,
138d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
148d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * See the License for the specific language governing permissions and
158d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * limitations under the License.
168d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
178d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *****************************************************************************
188d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
198d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S*/
208d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
218d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/*****************************************************************************/
228d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/* File Includes                                                             */
238d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/*****************************************************************************/
248d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
258d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/* System include files */
268d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S#include <stdio.h>
278d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
288d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/* User include files */
298d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S#include "irc_datatypes.h"
308d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S#include "irc_common.h"
318d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S#include "irc_cntrl_param.h"
328d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S#include "irc_mem_req_and_acq.h"
338d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S#include "irc_fixed_point_error_bits.h"
348d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
358d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Stypedef struct error_bits_t
368d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S{
378d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* Max tgt frm rate so that dynamic change in frm rate can be handled */
388d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_max_tgt_frm_rate;
398d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
408d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* Cur frm rate */
418d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_cur_tgt_frm_rate;
428d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
438d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* tgt frame rate*/
448d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_tgt_frm_rate;
458d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
468d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* tgt frm rate increment */
478d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_tgt_frm_rate_incr;
488d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
498d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* flag to indicate 1 second is up */
508d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    UWORD8 u1_compute_error_bits;
518d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
528d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* Bitrate/frame rate value added over a period */
538d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_accum_bitrate;
548d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
558d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* bitrate */
568d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_bitrate;
578d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
588d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S} error_bits_t;
598d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
608d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha SWORD32 irc_error_bits_num_fill_use_free_memtab(error_bits_t **pps_error_bits,
618d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                                               itt_memtab_t *ps_memtab,
628d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                                               ITT_FUNC_TYPE_E e_func_type)
638d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S{
648d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_mem_tab_idx = 0;
65c22addc6ee9c96f2996001362dbe39dd5e14038dMartin Storsjo    error_bits_t s_error_bits_temp;
668d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
678d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /*
688d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     * Hack for all alloc, during which we don't have any state memory.
698d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     * Dereferencing can cause issues
708d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     */
718d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
728d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        (*pps_error_bits) = &s_error_bits_temp;
738d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
748d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* For src rate control state structure */
758d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    if(e_func_type != GET_NUM_MEMTAB)
768d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    {
778d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(error_bits_t),
788d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                    ALIGN_128_BYTE, PERSISTENT, DDR);
798d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        use_or_fill_base(&ps_memtab[0], (void**)pps_error_bits, e_func_type);
808d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
818d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    i4_mem_tab_idx++;
828d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
838d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    return (i4_mem_tab_idx);
848d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
858d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
868d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/*******************************************************************************
878d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * @brief Calculates the error bits due to fixed point divisions
888d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S ******************************************************************************/
898d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Svoid irc_init_error_bits(error_bits_t *ps_error_bits,
908d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                         WORD32 i4_max_tgt_frm_rate,
918d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                         WORD32 i4_bitrate)
928d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S{
938d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* Initializing the parameters*/
948d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_cur_tgt_frm_rate = 0;
958d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_max_tgt_frm_rate = i4_max_tgt_frm_rate;
968d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
978d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* Value by which i4_cur_tgt_frm_rate is incremented every VOP*/
988d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_tgt_frm_rate_incr = 1000;
998d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1008d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /*Compute error bits is set to 1 at the end of 1 second*/
1018d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->u1_compute_error_bits = 0;
1028d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_tgt_frm_rate = i4_max_tgt_frm_rate;
1038d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_accum_bitrate = 0;
1048d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_bitrate = i4_bitrate;
1058d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
1068d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1078d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/*******************************************************************************
1088d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * @brief Updates the error state
1098d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S ******************************************************************************/
1108d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Svoid irc_update_error_bits(error_bits_t *ps_error_bits)
1118d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S{
1128d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_bits_per_frame;
1138d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1148d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    X_PROD_Y_DIV_Z(ps_error_bits->i4_bitrate, 1000,
1158d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                   ps_error_bits->i4_tgt_frm_rate, i4_bits_per_frame);
1168d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1178d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /*
1188d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     * This value is incremented every at the end of every VOP by
1198d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     * i4_tgt_frm_rate_incr
1208d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     */
1218d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_cur_tgt_frm_rate += ps_error_bits->i4_tgt_frm_rate_incr;
1228d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    if(ps_error_bits->u1_compute_error_bits == 1)
1238d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    {
1248d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        ps_error_bits->i4_accum_bitrate = 0;
1258d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
1268d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_accum_bitrate += i4_bits_per_frame;
1278d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1288d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /*
1298d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     * When current tgt frm rate is equal or greater than max tgt frame rate
1308d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     * 1 second is up , compute the error bits
1318d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S     */
1328d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    if(ps_error_bits->i4_cur_tgt_frm_rate >= ps_error_bits->i4_max_tgt_frm_rate)
1338d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    {
1348d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        ps_error_bits->i4_cur_tgt_frm_rate -=
1358d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                        ps_error_bits->i4_max_tgt_frm_rate;
1368d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        ps_error_bits->u1_compute_error_bits = 1;
1378d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
1388d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    else
1398d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    {
1408d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        ps_error_bits->u1_compute_error_bits = 0;
1418d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
1428d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
1438d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1448d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/*******************************************************************************
1458d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * @brief Returns the error bits for the current frame if there are any
1468d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
1478d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S ******************************************************************************/
1488d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha SWORD32 irc_get_error_bits(error_bits_t *ps_error_bits)
1498d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S{
1508d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    WORD32 i4_error_bits = 0;
1518d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1528d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /*If 1s is up calculate error for the last 1s worth of frames*/
1538d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    if(ps_error_bits->u1_compute_error_bits == 1)
1548d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    {
1558d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        /*Error = Actual bitrate - bits_per_frame * num of frames*/
1568d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S        i4_error_bits = ps_error_bits->i4_bitrate
1578d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                        - ps_error_bits->i4_accum_bitrate;
1588d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    }
1598d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1608d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    return (i4_error_bits);
1618d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
1628d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1638d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/* *****************************************************************************
1648d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
1658d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * @brief Change the frame rate parameter for the error bits state
1668d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S *
1678d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S ******************************************************************************/
1688d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Svoid irc_change_frm_rate_in_error_bits(error_bits_t *ps_error_bits,
1698d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                                       WORD32 i4_tgt_frm_rate)
1708d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S{
1718d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    /* Value by which i4_cur_tgt_frm_rate is incremented every VOP*/
1728d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_tgt_frm_rate_incr = (ps_error_bits->i4_max_tgt_frm_rate
1738d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                                           * 1000) / i4_tgt_frm_rate;
1748d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_tgt_frm_rate = i4_tgt_frm_rate;
1758d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
1768d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
1778d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S/*******************************************************************************
1788d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S * @brief Change the bitrate value for error bits module
1798d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S ******************************************************************************/
1808d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha Svoid irc_change_bitrate_in_error_bits(error_bits_t *ps_error_bits,
1818d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S                                      WORD32 i4_bitrate)
1828d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S{
1838d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S    ps_error_bits->i4_bitrate = i4_bitrate;
1848d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S}
1858d3d303c7942ced6a987a52db8977d768dc3605fHamsalekha S
186