00001 // 00002 // (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use. 00003 // 00004 00005 00006 #ifndef CHARLS_CONTEXTRUNMODE 00007 #define CHARLS_CONTEXTRUNMODE 00008 00009 // Implements statistical modelling for the run mode context. 00010 // Computes model dependent parameters like the golomb code lengths 00011 00012 struct CContextRunMode 00013 { 00014 CContextRunMode(LONG a, LONG nRItype, LONG nReset) : 00015 A(a), 00016 N(1), 00017 Nn(0), 00018 _nRItype(nRItype), 00019 _nReset((BYTE)nReset) 00020 { 00021 } 00022 00023 LONG A; 00024 BYTE N; 00025 BYTE Nn; 00026 LONG _nRItype; 00027 BYTE _nReset; 00028 00029 CContextRunMode() 00030 {} 00031 00032 00033 inlinehint LONG GetGolomb() const 00034 { 00035 LONG Ntest = N; 00036 LONG TEMP = A + (N >> 1) * _nRItype; 00037 LONG k = 0; 00038 for(; Ntest < TEMP; k++) 00039 { 00040 Ntest <<= 1; 00041 ASSERT(k <= 32); 00042 }; 00043 return k; 00044 } 00045 00046 00047 void UpdateVariables(LONG Errval, LONG EMErrval) 00048 { 00049 if (Errval < 0) 00050 { 00051 Nn = Nn + 1; 00052 } 00053 A = A + ((EMErrval + 1 - _nRItype) >> 1); 00054 if (N == _nReset) 00055 { 00056 A = A >> 1; 00057 N = N >> 1; 00058 Nn = Nn >> 1; 00059 } 00060 N = N + 1; 00061 } 00062 00063 inlinehint LONG ComputeErrVal(LONG temp, LONG k) 00064 { 00065 bool map = temp & 1; 00066 00067 LONG errvalabs = (temp + map) / 2; 00068 00069 if ((k != 0 || (2 * Nn >= N)) == map) 00070 { 00071 ASSERT(map == ComputeMap(-errvalabs, k)); 00072 return -errvalabs; 00073 } 00074 00075 ASSERT(map == ComputeMap(errvalabs, k)); 00076 return errvalabs; 00077 } 00078 00079 00080 bool ComputeMap(LONG Errval, LONG k) const 00081 { 00082 if ((k == 0) && (Errval > 0) && (2 * Nn < N)) 00083 return 1; 00084 00085 else if ((Errval < 0) && (2 * Nn >= N)) 00086 return 1; 00087 00088 else if ((Errval < 0) && (k != 0)) 00089 return 1; 00090 00091 return 0; 00092 } 00093 00094 00095 inlinehint LONG ComputeMapNegativeE(LONG k) const 00096 { 00097 return k != 0 || (2 * Nn >= N ); 00098 } 00099 }; 00100 00101 #endif