H264Cabac.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. -
  16. - Description : H264 CABAC context table initialization
  17. -
  18. ------------------------------------------------------------------------------*/
  19. #include "H264Cabac.h"
  20. #include "H264CabacContext.h"
  21. #include "enccommon.h"
  22. #include "enccfg.h"
  23. #define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
  24. static void SwapEndianess(u32 * buf, u32 sizeBytes)
  25. {
  26. #if (ENC8290_OUTPUT_SWAP_8 == 1)
  27. u32 i = 0;
  28. i32 words = sizeBytes / 4;
  29. ASSERT((sizeBytes % 8) == 0);
  30. while(words > 0)
  31. {
  32. u32 val = buf[i];
  33. u32 tmp = 0;
  34. tmp |= (val & 0xFF) << 24;
  35. tmp |= (val & 0xFF00) << 8;
  36. tmp |= (val & 0xFF0000) >> 8;
  37. tmp |= (val & 0xFF000000) >> 24;
  38. #if(ENC8290_OUTPUT_SWAP_32 == 1) /* need this for 64-bit HW */
  39. {
  40. u32 val2 = buf[i + 1];
  41. u32 tmp2 = 0;
  42. tmp2 |= (val2 & 0xFF) << 24;
  43. tmp2 |= (val2 & 0xFF00) << 8;
  44. tmp2 |= (val2 & 0xFF0000) >> 8;
  45. tmp2 |= (val2 & 0xFF000000) >> 24;
  46. buf[i] = tmp2;
  47. words--;
  48. i++;
  49. }
  50. #endif
  51. buf[i] = tmp;
  52. words--;
  53. i++;
  54. }
  55. #endif
  56. }
  57. #ifndef H8290_MEMCPY_CABAC_INIT
  58. u32 H264CabacInit(u32 * contextTable, u32 cabac_init_idc)
  59. {
  60. const i32(*ctx)[460][2];
  61. int i, j, qp;
  62. u8 *table = (u8 *) contextTable;
  63. for(qp = 0; qp < 52; qp++) /* All QP values */
  64. {
  65. for(j = 0; j < 2; j++) /* Intra/Inter */
  66. {
  67. if(j == 0)
  68. ctx = /*lint -e(545) */ &h264ContextInitIntra;
  69. else
  70. ctx = /*lint -e(545) */ &h264ContextInit[cabac_init_idc];
  71. for(i = 0; i < 460; i++)
  72. {
  73. i32 m = (i32) (*ctx)[i][0];
  74. i32 n = (i32) (*ctx)[i][1];
  75. i32 preCtxState = CLIP3(1, 126, ((m * (i32) qp) >> 4) + n);
  76. if(preCtxState <= 63)
  77. {
  78. table[qp * 464 * 2 + j * 464 + i] =
  79. (u8) ((63 - preCtxState) << 1);
  80. }
  81. else
  82. {
  83. table[qp * 464 * 2 + j * 464 + i] =
  84. (u8) (((preCtxState - 64) << 1) | 1);
  85. }
  86. }
  87. }
  88. }
  89. SwapEndianess(contextTable, 52 * 2 * 464);
  90. return 0;
  91. }
  92. #else
  93. u32 H264CabacInit(u32 * contextTable, u32 cabac_init_idc)
  94. {
  95. const i32(*ctx)[460][2];
  96. int i, j, qp;
  97. u8 *tempCabacTable = (u8*)EWLmalloc(52 * 2 * 464);
  98. if(tempCabacTable == NULL)
  99. {
  100. return 1;
  101. }
  102. for(qp = 0; qp < 52; qp++) /* All QP values */
  103. {
  104. for(j = 0; j < 2; j++) /* Intra/Inter */
  105. {
  106. if(j == 0)
  107. ctx = /*lint -e(545) */ &h264ContextInitIntra;
  108. else
  109. ctx = /*lint -e(545) */ &h264ContextInit[cabac_init_idc];
  110. for(i = 0; i < 460; i++)
  111. {
  112. i32 m = (i32) (*ctx)[i][0];
  113. i32 n = (i32) (*ctx)[i][1];
  114. i32 preCtxState = CLIP3(1, 126, ((m * (i32) qp) >> 4) + n);
  115. if(preCtxState <= 63)
  116. {
  117. tempCabacTable [qp * 464 * 2 + j * 464 + i] =
  118. (u8) ((63 - preCtxState) << 1);
  119. }
  120. else
  121. {
  122. tempCabacTable [qp * 464 * 2 + j * 464 + i] =
  123. (u8) (((preCtxState - 64) << 1) | 1);
  124. }
  125. }
  126. }
  127. }
  128. SwapEndianess((u32*)tempCabacTable , 52 * 2 * 464);
  129. EWLmemcpy(contextTable, tempCabacTable, 52 * 2 * 464);
  130. EWLfree(tempCabacTable);
  131. return 0;
  132. }
  133. #endif