| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bxdm_pp_avg.c $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/1 $ |
|---|
| 12 | * $brcm_Date: 2/16/10 10:50a $ |
|---|
| 13 | * |
|---|
| 14 | * [File Description:] |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/commonutils/xdm/bxdm_pp_avg.c $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/1 2/16/10 10:50a nilesh |
|---|
| 21 | * SW7405-2993: Initial XDM version |
|---|
| 22 | * |
|---|
| 23 | ***************************************************************************/ |
|---|
| 24 | |
|---|
| 25 | #include "bstd.h" |
|---|
| 26 | #include "bkni.h" |
|---|
| 27 | #include "bdbg.h" |
|---|
| 28 | #include "berr.h" |
|---|
| 29 | #include "bxdm_pp_fp.h" |
|---|
| 30 | #include "bxdm_pp_avg.h" |
|---|
| 31 | |
|---|
| 32 | BDBG_MODULE(BXDM_PPAVG); |
|---|
| 33 | |
|---|
| 34 | const char BXDM_PictureProvider_P_DISPMGR_AVG_NODE[]="DMAVG:\t""$brcm_Revision: Hydra_Software_Devel/1 $"; |
|---|
| 35 | |
|---|
| 36 | typedef struct BXDM_PPAVG_P_Context |
|---|
| 37 | { |
|---|
| 38 | BXDM_PPAVG_P_Settings stSettings; |
|---|
| 39 | |
|---|
| 40 | uint32_t uiSampleCount; |
|---|
| 41 | uint32_t uiSampleIndex; |
|---|
| 42 | BXDM_PPFP_P_DataType *astSample; |
|---|
| 43 | BXDM_PPFP_P_DataType stCumulativeSum; |
|---|
| 44 | } BXDM_PPAVG_P_Context; |
|---|
| 45 | |
|---|
| 46 | static const BXDM_PPAVG_P_Settings s_stAvgDefaultSettings = |
|---|
| 47 | { |
|---|
| 48 | 120, /* Samples */ |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | BERR_Code |
|---|
| 52 | BXDM_PPAVG_P_GetDefaultSettings( |
|---|
| 53 | BXDM_PPAVG_P_Settings *pstAvgSettings |
|---|
| 54 | ) |
|---|
| 55 | { |
|---|
| 56 | BDBG_ENTER(BXDM_PPAVG_P_GetDefaultSettings); |
|---|
| 57 | |
|---|
| 58 | BDBG_ASSERT(pstAvgSettings); |
|---|
| 59 | |
|---|
| 60 | *pstAvgSettings = s_stAvgDefaultSettings; |
|---|
| 61 | |
|---|
| 62 | BDBG_LEAVE(BXDM_PPAVG_P_GetDefaultSettings); |
|---|
| 63 | |
|---|
| 64 | return BERR_TRACE(BERR_SUCCESS); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | BERR_Code |
|---|
| 68 | BXDM_PPAVG_P_Create( |
|---|
| 69 | BXDM_PPAVG_P_Handle *phAvg, |
|---|
| 70 | const BXDM_PPAVG_P_Settings *pAvgSettings |
|---|
| 71 | ) |
|---|
| 72 | { |
|---|
| 73 | BXDM_PPAVG_P_Context *pAvgHandle = NULL; |
|---|
| 74 | |
|---|
| 75 | BDBG_ENTER(BXDM_PPAVG_P_Create); |
|---|
| 76 | |
|---|
| 77 | BDBG_ASSERT(phAvg); |
|---|
| 78 | BDBG_ASSERT(pAvgSettings); |
|---|
| 79 | BDBG_ASSERT(pAvgSettings->uiNumSamples); |
|---|
| 80 | |
|---|
| 81 | /* Set handle to NULL in case the allocation fails */ |
|---|
| 82 | *phAvg = NULL; |
|---|
| 83 | |
|---|
| 84 | /* Allocate AVG Handle */ |
|---|
| 85 | pAvgHandle = (BXDM_PPAVG_P_Context *) BKNI_Malloc(sizeof(BXDM_PPAVG_P_Context)); |
|---|
| 86 | if (!pAvgHandle) |
|---|
| 87 | { |
|---|
| 88 | return BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /* Zero out the newly allocated context */ |
|---|
| 92 | BKNI_Memset((void*)pAvgHandle, 0, sizeof(BXDM_PPAVG_P_Context)); |
|---|
| 93 | |
|---|
| 94 | /* Allocate value queue */ |
|---|
| 95 | pAvgHandle->stSettings = *pAvgSettings; |
|---|
| 96 | |
|---|
| 97 | pAvgHandle->astSample = (BXDM_PPFP_P_DataType *) BKNI_Malloc(sizeof(BXDM_PPFP_P_DataType) * pAvgHandle->stSettings.uiNumSamples); |
|---|
| 98 | if (!pAvgHandle->astSample) |
|---|
| 99 | { |
|---|
| 100 | BXDM_PPAVG_P_Destroy(pAvgHandle); |
|---|
| 101 | return BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY); |
|---|
| 102 | } |
|---|
| 103 | BKNI_Memset((void*)pAvgHandle->astSample, 0, (sizeof(BXDM_PPFP_P_DataType) * pAvgHandle->stSettings.uiNumSamples)); |
|---|
| 104 | |
|---|
| 105 | *phAvg = pAvgHandle; |
|---|
| 106 | |
|---|
| 107 | BDBG_LEAVE(BXDM_PPAVG_P_Create); |
|---|
| 108 | |
|---|
| 109 | return BERR_TRACE(BERR_SUCCESS); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | BERR_Code |
|---|
| 113 | BXDM_PPAVG_P_Destroy( |
|---|
| 114 | BXDM_PPAVG_P_Handle hAvg |
|---|
| 115 | ) |
|---|
| 116 | { |
|---|
| 117 | BDBG_ENTER(BXDM_PPAVG_P_Destroy); |
|---|
| 118 | |
|---|
| 119 | BDBG_ASSERT(hAvg); |
|---|
| 120 | |
|---|
| 121 | if ( hAvg->astSample ) |
|---|
| 122 | { |
|---|
| 123 | BKNI_Free(hAvg->astSample); |
|---|
| 124 | hAvg->astSample = NULL; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | BKNI_Free(hAvg); |
|---|
| 128 | |
|---|
| 129 | BDBG_LEAVE(BXDM_PPAVG_P_Destroy); |
|---|
| 130 | |
|---|
| 131 | return BERR_TRACE(BERR_SUCCESS); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | BERR_Code |
|---|
| 135 | BXDM_PPAVG_P_AddValueFixedPoint( |
|---|
| 136 | BXDM_PPAVG_P_Handle hAvg, |
|---|
| 137 | const BXDM_PPFP_P_DataType *pstValue |
|---|
| 138 | ) |
|---|
| 139 | { |
|---|
| 140 | BDBG_ENTER(BXDM_PPAVG_P_AddValueFixedPoint); |
|---|
| 141 | |
|---|
| 142 | BDBG_ASSERT(hAvg); |
|---|
| 143 | BDBG_ASSERT(pstValue); |
|---|
| 144 | |
|---|
| 145 | /* Compute Cumulative Sum */ |
|---|
| 146 | if ( hAvg->uiSampleCount < hAvg->stSettings.uiNumSamples ) |
|---|
| 147 | { |
|---|
| 148 | /* Increment Sample Count */ |
|---|
| 149 | hAvg->uiSampleCount++; |
|---|
| 150 | } |
|---|
| 151 | else |
|---|
| 152 | { |
|---|
| 153 | /* Subtract oldest value from running sum */ |
|---|
| 154 | BXDM_PPFP_P_FixPtSub( |
|---|
| 155 | &hAvg->stCumulativeSum, |
|---|
| 156 | &hAvg->astSample[hAvg->uiSampleIndex], |
|---|
| 157 | &hAvg->stCumulativeSum); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | hAvg->astSample[hAvg->uiSampleIndex] = *pstValue; |
|---|
| 161 | |
|---|
| 162 | /* Update running sum */ |
|---|
| 163 | BXDM_PPFP_P_FixPtAdd( |
|---|
| 164 | &hAvg->stCumulativeSum, |
|---|
| 165 | &hAvg->astSample[hAvg->uiSampleIndex], |
|---|
| 166 | &hAvg->stCumulativeSum); |
|---|
| 167 | |
|---|
| 168 | /* Increment error index */ |
|---|
| 169 | hAvg->uiSampleIndex++; |
|---|
| 170 | if ( hAvg->uiSampleIndex >= hAvg->stSettings.uiNumSamples ) |
|---|
| 171 | { |
|---|
| 172 | hAvg->uiSampleIndex = 0; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | BDBG_LEAVE(BXDM_PPAVG_P_AddValueFixedPoint); |
|---|
| 176 | |
|---|
| 177 | return BERR_TRACE( BERR_SUCCESS ); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | BERR_Code |
|---|
| 181 | BXDM_PPAVG_P_AddValueInteger( |
|---|
| 182 | BXDM_PPAVG_P_Handle hAvg, |
|---|
| 183 | int32_t iValue |
|---|
| 184 | ) |
|---|
| 185 | { |
|---|
| 186 | BERR_Code rc; |
|---|
| 187 | BXDM_PPFP_P_DataType stValue; |
|---|
| 188 | |
|---|
| 189 | BDBG_ENTER(BXDM_PPAVG_P_AddValueInteger); |
|---|
| 190 | |
|---|
| 191 | BDBG_ASSERT(hAvg); |
|---|
| 192 | |
|---|
| 193 | stValue.uiWhole = (uint32_t) iValue; |
|---|
| 194 | stValue.uiFractional = 0; |
|---|
| 195 | |
|---|
| 196 | rc = BXDM_PPAVG_P_AddValueFixedPoint( |
|---|
| 197 | hAvg, |
|---|
| 198 | &stValue |
|---|
| 199 | ); |
|---|
| 200 | |
|---|
| 201 | BDBG_LEAVE(BXDM_PPAVG_P_AddValueInteger); |
|---|
| 202 | |
|---|
| 203 | return BERR_TRACE( rc ); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | BERR_Code |
|---|
| 207 | BXDM_PPAVG_P_Reset( |
|---|
| 208 | BXDM_PPAVG_P_Handle hAvg |
|---|
| 209 | ) |
|---|
| 210 | { |
|---|
| 211 | BDBG_ENTER(BXDM_PPAVG_P_Reset); |
|---|
| 212 | |
|---|
| 213 | BDBG_ASSERT(hAvg); |
|---|
| 214 | |
|---|
| 215 | hAvg->uiSampleCount = 0; |
|---|
| 216 | hAvg->uiSampleIndex = 0; |
|---|
| 217 | BKNI_Memset((void*)hAvg->astSample, 0, (sizeof(BXDM_PPFP_P_DataType) * hAvg->stSettings.uiNumSamples)); |
|---|
| 218 | |
|---|
| 219 | hAvg->stCumulativeSum.uiWhole = 0; |
|---|
| 220 | hAvg->stCumulativeSum.uiFractional = 0; |
|---|
| 221 | |
|---|
| 222 | BDBG_LEAVE(BXDM_PPAVG_P_Reset); |
|---|
| 223 | |
|---|
| 224 | return BERR_TRACE(BERR_SUCCESS); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | BERR_Code |
|---|
| 228 | BXDM_PPAVG_P_GetAverage( |
|---|
| 229 | BXDM_PPAVG_P_Handle hAvg, |
|---|
| 230 | BXDM_PPFP_P_DataType *pstAverage, |
|---|
| 231 | uint32_t *puiNumSamples |
|---|
| 232 | ) |
|---|
| 233 | { |
|---|
| 234 | BDBG_ENTER( BXDM_PPAVG_P_GetAverage ); |
|---|
| 235 | |
|---|
| 236 | BDBG_ASSERT( hAvg ); |
|---|
| 237 | BDBG_ASSERT( pstAverage ); |
|---|
| 238 | |
|---|
| 239 | if ( NULL != puiNumSamples ) |
|---|
| 240 | { |
|---|
| 241 | *puiNumSamples = hAvg->uiSampleCount; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | if ( 0 == hAvg->uiSampleCount ) |
|---|
| 245 | { |
|---|
| 246 | pstAverage->uiWhole = 0; |
|---|
| 247 | pstAverage->uiFractional = 0; |
|---|
| 248 | } |
|---|
| 249 | else |
|---|
| 250 | { |
|---|
| 251 | bool bNegative = false; |
|---|
| 252 | |
|---|
| 253 | /* Compute Average */ |
|---|
| 254 | *pstAverage = hAvg->stCumulativeSum; |
|---|
| 255 | |
|---|
| 256 | if ( (int32_t) pstAverage->uiWhole < 0 ) |
|---|
| 257 | { |
|---|
| 258 | bNegative = true; |
|---|
| 259 | pstAverage->uiWhole = (uint32_t) (- ((int32_t) pstAverage->uiWhole)); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | BXDM_PPFP_P_FixPtDiv( |
|---|
| 263 | pstAverage, |
|---|
| 264 | hAvg->uiSampleCount, |
|---|
| 265 | pstAverage); |
|---|
| 266 | |
|---|
| 267 | if ( true == bNegative ) |
|---|
| 268 | { |
|---|
| 269 | pstAverage->uiWhole = (uint32_t) (- ((int32_t) pstAverage->uiWhole)); |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | BDBG_LEAVE( BXDM_PPAVG_P_GetAverage ); |
|---|
| 274 | |
|---|
| 275 | return BERR_TRACE( BERR_SUCCESS ); |
|---|
| 276 | } |
|---|