You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3043 lines
101KB

  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "avcodec.h"
  19. #include "common.h"
  20. #include "dsputil.h"
  21. #include "cabac.h"
  22. #include "mpegvideo.h"
  23. #undef NDEBUG
  24. #include <assert.h>
  25. #define MAX_DECOMPOSITIONS 8
  26. #define MAX_PLANES 4
  27. #define DWTELEM int
  28. #define QROOT 8
  29. #define LOSSLESS_QLOG -128
  30. static const int8_t quant3[256]={
  31. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  32. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  33. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  34. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  35. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  36. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  40. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  41. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  42. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  43. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  44. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  45. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  46. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
  47. };
  48. static const int8_t quant3b[256]={
  49. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  50. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  57. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  58. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  59. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  60. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  61. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  62. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  63. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  64. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  65. };
  66. static const int8_t quant5[256]={
  67. 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  68. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  69. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  70. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  71. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  72. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  73. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  74. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  75. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  76. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  77. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  78. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  79. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  80. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  81. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  82. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
  83. };
  84. static const int8_t quant7[256]={
  85. 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  86. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  87. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  88. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  89. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  90. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  91. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  92. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  93. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  94. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  95. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  96. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  97. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  98. -3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,
  99. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  100. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
  101. };
  102. static const int8_t quant9[256]={
  103. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  104. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  105. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  106. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  107. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  108. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  109. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  110. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  111. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  112. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  113. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  114. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  115. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  116. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  117. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  118. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  119. };
  120. static const int8_t quant11[256]={
  121. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  122. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  123. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  124. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  125. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  126. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  127. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  128. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  129. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  130. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  131. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  132. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  133. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  134. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
  135. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  136. -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
  137. };
  138. static const int8_t quant13[256]={
  139. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  140. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  141. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  142. 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  143. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  144. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  145. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  146. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  147. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  148. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  149. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  150. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  151. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  152. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  153. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  154. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  155. };
  156. #define OBMC_MAX 64
  157. #if 0 //64*cubic
  158. static const uint8_t obmc32[1024]={
  159. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  160. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  161. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  162. 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
  163. 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
  164. 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
  165. 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
  166. 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
  167. 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
  168. 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
  169. 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
  170. 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
  171. 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
  172. 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
  173. 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
  174. 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
  175. 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
  176. 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
  177. 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
  178. 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
  179. 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
  180. 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
  181. 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
  182. 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
  183. 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
  184. 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
  185. 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
  186. 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
  187. 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
  188. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  189. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  190. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  191. //error:0.000022
  192. };
  193. static const uint8_t obmc16[256]={
  194. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  195. 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
  196. 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
  197. 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
  198. 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
  199. 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
  200. 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
  201. 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
  202. 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
  203. 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
  204. 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
  205. 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
  206. 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
  207. 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
  208. 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
  209. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  210. //error:0.000033
  211. };
  212. #elif 1 // 64*linear
  213. static const uint8_t obmc32[1024]={
  214. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  215. 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
  216. 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
  217. 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
  218. 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
  219. 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
  220. 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
  221. 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
  222. 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
  223. 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
  224. 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
  225. 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
  226. 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
  227. 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
  228. 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
  229. 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
  230. 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
  231. 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
  232. 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
  233. 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
  234. 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
  235. 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
  236. 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
  237. 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
  238. 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
  239. 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
  240. 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
  241. 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
  242. 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
  243. 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
  244. 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
  245. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  246. //error:0.000020
  247. };
  248. static const uint8_t obmc16[256]={
  249. 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
  250. 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
  251. 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
  252. 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
  253. 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
  254. 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
  255. 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
  256. 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
  257. 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
  258. 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
  259. 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
  260. 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
  261. 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
  262. 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
  263. 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
  264. 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
  265. //error:0.000015
  266. };
  267. #else //64*cos
  268. static const uint8_t obmc32[1024]={
  269. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  270. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  271. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  272. 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
  273. 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
  274. 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
  275. 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
  276. 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
  277. 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
  278. 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
  279. 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
  280. 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
  281. 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
  282. 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
  283. 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
  284. 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
  285. 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
  286. 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
  287. 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
  288. 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
  289. 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
  290. 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
  291. 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
  292. 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
  293. 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
  294. 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
  295. 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
  296. 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
  297. 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
  298. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  299. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  300. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  301. //error:0.000022
  302. };
  303. static const uint8_t obmc16[256]={
  304. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  305. 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
  306. 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
  307. 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
  308. 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
  309. 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
  310. 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
  311. 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
  312. 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
  313. 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
  314. 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
  315. 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
  316. 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
  317. 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
  318. 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
  319. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  320. //error:0.000022
  321. };
  322. #endif
  323. //linear *64
  324. static const uint8_t obmc8[64]={
  325. 1, 3, 5, 7, 7, 5, 3, 1,
  326. 3, 9,15,21,21,15, 9, 3,
  327. 5,15,25,35,35,25,15, 5,
  328. 7,21,35,49,49,35,21, 7,
  329. 7,21,35,49,49,35,21, 7,
  330. 5,15,25,35,35,25,15, 5,
  331. 3, 9,15,21,21,15, 9, 3,
  332. 1, 3, 5, 7, 7, 5, 3, 1,
  333. //error:0.000000
  334. };
  335. //linear *64
  336. static const uint8_t obmc4[16]={
  337. 4,12,12, 4,
  338. 12,36,36,12,
  339. 12,36,36,12,
  340. 4,12,12, 4,
  341. //error:0.000000
  342. };
  343. static const uint8_t *obmc_tab[4]={
  344. obmc32, obmc16, obmc8, obmc4
  345. };
  346. typedef struct BlockNode{
  347. int16_t mx;
  348. int16_t my;
  349. uint8_t color[3];
  350. uint8_t type;
  351. //#define TYPE_SPLIT 1
  352. #define BLOCK_INTRA 1
  353. //#define TYPE_NOCOLOR 4
  354. uint8_t level; //FIXME merge into type?
  355. }BlockNode;
  356. #define LOG2_MB_SIZE 4
  357. #define MB_SIZE (1<<LOG2_MB_SIZE)
  358. typedef struct SubBand{
  359. int level;
  360. int stride;
  361. int width;
  362. int height;
  363. int qlog; ///< log(qscale)/log[2^(1/6)]
  364. DWTELEM *buf;
  365. int16_t *x;
  366. DWTELEM *coeff;
  367. struct SubBand *parent;
  368. uint8_t state[/*7*2*/ 7 + 512][32];
  369. }SubBand;
  370. typedef struct Plane{
  371. int width;
  372. int height;
  373. SubBand band[MAX_DECOMPOSITIONS][4];
  374. }Plane;
  375. typedef struct SnowContext{
  376. // MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
  377. AVCodecContext *avctx;
  378. CABACContext c;
  379. DSPContext dsp;
  380. AVFrame input_picture;
  381. AVFrame current_picture;
  382. AVFrame last_picture;
  383. AVFrame mconly_picture;
  384. // uint8_t q_context[16];
  385. uint8_t header_state[32];
  386. uint8_t block_state[128 + 32*128];
  387. int keyframe;
  388. int version;
  389. int spatial_decomposition_type;
  390. int temporal_decomposition_type;
  391. int spatial_decomposition_count;
  392. int temporal_decomposition_count;
  393. DWTELEM *spatial_dwt_buffer;
  394. DWTELEM *pred_buffer;
  395. int colorspace_type;
  396. int chroma_h_shift;
  397. int chroma_v_shift;
  398. int spatial_scalability;
  399. int qlog;
  400. int lambda;
  401. int lambda2;
  402. int mv_scale;
  403. int qbias;
  404. #define QBIAS_SHIFT 3
  405. int b_width;
  406. int b_height;
  407. int block_max_depth;
  408. Plane plane[MAX_PLANES];
  409. BlockNode *block;
  410. MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
  411. }SnowContext;
  412. #define QEXPSHIFT 7 //FIXME try to change this to 0
  413. static const uint8_t qexp[8]={
  414. 128, 140, 152, 166, 181, 197, 215, 235
  415. // 64, 70, 76, 83, 91, 99, 108, 117
  416. // 32, 35, 38, 41, 45, 49, 54, 59
  417. // 16, 17, 19, 21, 23, 25, 27, 29
  418. // 8, 9, 10, 10, 11, 12, 13, 15
  419. };
  420. static inline int mirror(int v, int m){
  421. if (v<0) return -v;
  422. else if(v>m) return 2*m-v;
  423. else return v;
  424. }
  425. static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){
  426. int i;
  427. if(v){
  428. const int a= ABS(v);
  429. const int e= av_log2(a);
  430. #if 1
  431. const int el= FFMIN(e, 10);
  432. put_cabac(c, state+0, 0);
  433. for(i=0; i<el; i++){
  434. put_cabac(c, state+1+i, 1); //1..10
  435. }
  436. for(; i<e; i++){
  437. put_cabac(c, state+1+9, 1); //1..10
  438. }
  439. put_cabac(c, state+1+FFMIN(i,9), 0);
  440. for(i=e-1; i>=el; i--){
  441. put_cabac(c, state+22+9, (a>>i)&1); //22..31
  442. }
  443. for(; i>=0; i--){
  444. put_cabac(c, state+22+i, (a>>i)&1); //22..31
  445. }
  446. if(is_signed)
  447. put_cabac(c, state+11 + el, v < 0); //11..21
  448. #else
  449. put_cabac(c, state+0, 0);
  450. if(e<=9){
  451. for(i=0; i<e; i++){
  452. put_cabac(c, state+1+i, 1); //1..10
  453. }
  454. put_cabac(c, state+1+i, 0);
  455. for(i=e-1; i>=0; i--){
  456. put_cabac(c, state+22+i, (a>>i)&1); //22..31
  457. }
  458. if(is_signed)
  459. put_cabac(c, state+11 + e, v < 0); //11..21
  460. }else{
  461. for(i=0; i<e; i++){
  462. put_cabac(c, state+1+FFMIN(i,9), 1); //1..10
  463. }
  464. put_cabac(c, state+1+FFMIN(i,9), 0);
  465. for(i=e-1; i>=0; i--){
  466. put_cabac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  467. }
  468. if(is_signed)
  469. put_cabac(c, state+11 + FFMIN(e,10), v < 0); //11..21
  470. }
  471. #endif
  472. }else{
  473. put_cabac(c, state+0, 1);
  474. }
  475. }
  476. static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed){
  477. if(get_cabac(c, state+0))
  478. return 0;
  479. else{
  480. int i, e, a, el;
  481. //FIXME try to merge loops with FFMIN() maybe they are equally fast and they are surly cuter
  482. for(e=0; e<10; e++){
  483. if(get_cabac(c, state + 1 + e)==0) // 1..10
  484. break;
  485. }
  486. el= e;
  487. if(e==10){
  488. while(get_cabac(c, state + 1 + 9)) //10
  489. e++;
  490. }
  491. a= 1;
  492. for(i=e-1; i>=el; i--){
  493. a += a + get_cabac(c, state+22+9); //31
  494. }
  495. for(; i>=0; i--){
  496. a += a + get_cabac(c, state+22+i); //22..31
  497. }
  498. if(is_signed && get_cabac(c, state+11 + el)) //11..21
  499. return -a;
  500. else
  501. return a;
  502. }
  503. }
  504. static inline void put_symbol2(CABACContext *c, uint8_t *state, int v, int log2){
  505. int i;
  506. int r= log2>=0 ? 1<<log2 : 1;
  507. assert(v>=0);
  508. assert(log2>=-4);
  509. while(v >= r){
  510. put_cabac(c, state+4+log2, 1);
  511. v -= r;
  512. log2++;
  513. if(log2>0) r+=r;
  514. }
  515. put_cabac(c, state+4+log2, 0);
  516. for(i=log2-1; i>=0; i--){
  517. put_cabac(c, state+31-i, (v>>i)&1);
  518. }
  519. }
  520. static inline int get_symbol2(CABACContext *c, uint8_t *state, int log2){
  521. int i;
  522. int r= log2>=0 ? 1<<log2 : 1;
  523. int v=0;
  524. assert(log2>=-4);
  525. while(get_cabac(c, state+4+log2)){
  526. v+= r;
  527. log2++;
  528. if(log2>0) r+=r;
  529. }
  530. for(i=log2-1; i>=0; i--){
  531. v+= get_cabac(c, state+31-i)<<i;
  532. }
  533. return v;
  534. }
  535. static always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
  536. const int mirror_left= !highpass;
  537. const int mirror_right= (width&1) ^ highpass;
  538. const int w= (width>>1) - 1 + (highpass & width);
  539. int i;
  540. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  541. if(mirror_left){
  542. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  543. dst += dst_step;
  544. src += src_step;
  545. }
  546. for(i=0; i<w; i++){
  547. dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
  548. }
  549. if(mirror_right){
  550. dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
  551. }
  552. }
  553. static always_inline void lift5(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
  554. const int mirror_left= !highpass;
  555. const int mirror_right= (width&1) ^ highpass;
  556. const int w= (width>>1) - 1 + (highpass & width);
  557. int i;
  558. if(mirror_left){
  559. int r= 3*2*ref[0];
  560. r += r>>4;
  561. r += r>>8;
  562. dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
  563. dst += dst_step;
  564. src += src_step;
  565. }
  566. for(i=0; i<w; i++){
  567. int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
  568. r += r>>4;
  569. r += r>>8;
  570. dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
  571. }
  572. if(mirror_right){
  573. int r= 3*2*ref[w*ref_step];
  574. r += r>>4;
  575. r += r>>8;
  576. dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
  577. }
  578. }
  579. static void inplace_lift(int *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
  580. int x, i;
  581. for(x=start; x<width; x+=2){
  582. int64_t sum=0;
  583. for(i=0; i<n; i++){
  584. int x2= x + 2*i - n + 1;
  585. if (x2< 0) x2= -x2;
  586. else if(x2>=width) x2= 2*width-x2-2;
  587. sum += coeffs[i]*(int64_t)dst[x2];
  588. }
  589. if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
  590. else dst[x] += (sum + (1<<shift)/2)>>shift;
  591. }
  592. }
  593. static void inplace_liftV(int *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
  594. int x, y, i;
  595. for(y=start; y<height; y+=2){
  596. for(x=0; x<width; x++){
  597. int64_t sum=0;
  598. for(i=0; i<n; i++){
  599. int y2= y + 2*i - n + 1;
  600. if (y2< 0) y2= -y2;
  601. else if(y2>=height) y2= 2*height-y2-2;
  602. sum += coeffs[i]*(int64_t)dst[x + y2*stride];
  603. }
  604. if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
  605. else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
  606. }
  607. }
  608. }
  609. #define SCALEX 1
  610. #define LX0 0
  611. #define LX1 1
  612. #if 0 // more accurate 9/7
  613. #define N1 2
  614. #define SHIFT1 14
  615. #define COEFFS1 (int[]){-25987,-25987}
  616. #define N2 2
  617. #define SHIFT2 19
  618. #define COEFFS2 (int[]){-27777,-27777}
  619. #define N3 2
  620. #define SHIFT3 15
  621. #define COEFFS3 (int[]){28931,28931}
  622. #define N4 2
  623. #define SHIFT4 15
  624. #define COEFFS4 (int[]){14533,14533}
  625. #elif 1 // 13/7 CRF
  626. #define N1 4
  627. #define SHIFT1 4
  628. #define COEFFS1 (int[]){1,-9,-9,1}
  629. #define N2 4
  630. #define SHIFT2 4
  631. #define COEFFS2 (int[]){-1,5,5,-1}
  632. #define N3 0
  633. #define SHIFT3 1
  634. #define COEFFS3 NULL
  635. #define N4 0
  636. #define SHIFT4 1
  637. #define COEFFS4 NULL
  638. #elif 1 // 3/5
  639. #define LX0 1
  640. #define LX1 0
  641. #define SCALEX 0.5
  642. #define N1 2
  643. #define SHIFT1 1
  644. #define COEFFS1 (int[]){1,1}
  645. #define N2 2
  646. #define SHIFT2 2
  647. #define COEFFS2 (int[]){-1,-1}
  648. #define N3 0
  649. #define SHIFT3 0
  650. #define COEFFS3 NULL
  651. #define N4 0
  652. #define SHIFT4 0
  653. #define COEFFS4 NULL
  654. #elif 1 // 11/5
  655. #define N1 0
  656. #define SHIFT1 1
  657. #define COEFFS1 NULL
  658. #define N2 2
  659. #define SHIFT2 2
  660. #define COEFFS2 (int[]){-1,-1}
  661. #define N3 2
  662. #define SHIFT3 0
  663. #define COEFFS3 (int[]){-1,-1}
  664. #define N4 4
  665. #define SHIFT4 7
  666. #define COEFFS4 (int[]){-5,29,29,-5}
  667. #define SCALEX 4
  668. #elif 1 // 9/7 CDF
  669. #define N1 2
  670. #define SHIFT1 7
  671. #define COEFFS1 (int[]){-203,-203}
  672. #define N2 2
  673. #define SHIFT2 12
  674. #define COEFFS2 (int[]){-217,-217}
  675. #define N3 2
  676. #define SHIFT3 7
  677. #define COEFFS3 (int[]){113,113}
  678. #define N4 2
  679. #define SHIFT4 9
  680. #define COEFFS4 (int[]){227,227}
  681. #define SCALEX 1
  682. #elif 1 // 7/5 CDF
  683. #define N1 0
  684. #define SHIFT1 1
  685. #define COEFFS1 NULL
  686. #define N2 2
  687. #define SHIFT2 2
  688. #define COEFFS2 (int[]){-1,-1}
  689. #define N3 2
  690. #define SHIFT3 0
  691. #define COEFFS3 (int[]){-1,-1}
  692. #define N4 2
  693. #define SHIFT4 4
  694. #define COEFFS4 (int[]){3,3}
  695. #elif 1 // 9/7 MN
  696. #define N1 4
  697. #define SHIFT1 4
  698. #define COEFFS1 (int[]){1,-9,-9,1}
  699. #define N2 2
  700. #define SHIFT2 2
  701. #define COEFFS2 (int[]){1,1}
  702. #define N3 0
  703. #define SHIFT3 1
  704. #define COEFFS3 NULL
  705. #define N4 0
  706. #define SHIFT4 1
  707. #define COEFFS4 NULL
  708. #else // 13/7 CRF
  709. #define N1 4
  710. #define SHIFT1 4
  711. #define COEFFS1 (int[]){1,-9,-9,1}
  712. #define N2 4
  713. #define SHIFT2 4
  714. #define COEFFS2 (int[]){-1,5,5,-1}
  715. #define N3 0
  716. #define SHIFT3 1
  717. #define COEFFS3 NULL
  718. #define N4 0
  719. #define SHIFT4 1
  720. #define COEFFS4 NULL
  721. #endif
  722. static void horizontal_decomposeX(int *b, int width){
  723. int temp[width];
  724. const int width2= width>>1;
  725. const int w2= (width+1)>>1;
  726. int A1,A2,A3,A4, x;
  727. inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
  728. inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
  729. inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
  730. inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
  731. for(x=0; x<width2; x++){
  732. temp[x ]= b[2*x ];
  733. temp[x+w2]= b[2*x + 1];
  734. }
  735. if(width&1)
  736. temp[x ]= b[2*x ];
  737. memcpy(b, temp, width*sizeof(int));
  738. }
  739. static void horizontal_composeX(int *b, int width){
  740. int temp[width];
  741. const int width2= width>>1;
  742. int A1,A2,A3,A4, x;
  743. const int w2= (width+1)>>1;
  744. memcpy(temp, b, width*sizeof(int));
  745. for(x=0; x<width2; x++){
  746. b[2*x ]= temp[x ];
  747. b[2*x + 1]= temp[x+w2];
  748. }
  749. if(width&1)
  750. b[2*x ]= temp[x ];
  751. inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
  752. inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
  753. inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
  754. inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
  755. }
  756. static void spatial_decomposeX(int *buffer, int width, int height, int stride){
  757. int x, y;
  758. for(y=0; y<height; y++){
  759. for(x=0; x<width; x++){
  760. buffer[y*stride + x] *= SCALEX;
  761. }
  762. }
  763. for(y=0; y<height; y++){
  764. horizontal_decomposeX(buffer + y*stride, width);
  765. }
  766. inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
  767. inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
  768. inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
  769. inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
  770. }
  771. static void spatial_composeX(int *buffer, int width, int height, int stride){
  772. int x, y;
  773. inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
  774. inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
  775. inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
  776. inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
  777. for(y=0; y<height; y++){
  778. horizontal_composeX(buffer + y*stride, width);
  779. }
  780. for(y=0; y<height; y++){
  781. for(x=0; x<width; x++){
  782. buffer[y*stride + x] /= SCALEX;
  783. }
  784. }
  785. }
  786. static void horizontal_decompose53i(int *b, int width){
  787. int temp[width];
  788. const int width2= width>>1;
  789. int A1,A2,A3,A4, x;
  790. const int w2= (width+1)>>1;
  791. for(x=0; x<width2; x++){
  792. temp[x ]= b[2*x ];
  793. temp[x+w2]= b[2*x + 1];
  794. }
  795. if(width&1)
  796. temp[x ]= b[2*x ];
  797. #if 0
  798. A2= temp[1 ];
  799. A4= temp[0 ];
  800. A1= temp[0+width2];
  801. A1 -= (A2 + A4)>>1;
  802. A4 += (A1 + 1)>>1;
  803. b[0+width2] = A1;
  804. b[0 ] = A4;
  805. for(x=1; x+1<width2; x+=2){
  806. A3= temp[x+width2];
  807. A4= temp[x+1 ];
  808. A3 -= (A2 + A4)>>1;
  809. A2 += (A1 + A3 + 2)>>2;
  810. b[x+width2] = A3;
  811. b[x ] = A2;
  812. A1= temp[x+1+width2];
  813. A2= temp[x+2 ];
  814. A1 -= (A2 + A4)>>1;
  815. A4 += (A1 + A3 + 2)>>2;
  816. b[x+1+width2] = A1;
  817. b[x+1 ] = A4;
  818. }
  819. A3= temp[width-1];
  820. A3 -= A2;
  821. A2 += (A1 + A3 + 2)>>2;
  822. b[width -1] = A3;
  823. b[width2-1] = A2;
  824. #else
  825. lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  826. lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  827. #endif
  828. }
  829. static void vertical_decompose53iH0(int *b0, int *b1, int *b2, int width){
  830. int i;
  831. for(i=0; i<width; i++){
  832. b1[i] -= (b0[i] + b2[i])>>1;
  833. }
  834. }
  835. static void vertical_decompose53iL0(int *b0, int *b1, int *b2, int width){
  836. int i;
  837. for(i=0; i<width; i++){
  838. b1[i] += (b0[i] + b2[i] + 2)>>2;
  839. }
  840. }
  841. static void spatial_decompose53i(int *buffer, int width, int height, int stride){
  842. int y;
  843. DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
  844. DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
  845. for(y=-2; y<height; y+=2){
  846. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  847. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  848. {START_TIMER
  849. if(b1 <= b3) horizontal_decompose53i(b2, width);
  850. if(y+2 < height) horizontal_decompose53i(b3, width);
  851. STOP_TIMER("horizontal_decompose53i")}
  852. {START_TIMER
  853. if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
  854. if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
  855. STOP_TIMER("vertical_decompose53i*")}
  856. b0=b2;
  857. b1=b3;
  858. }
  859. }
  860. #define lift5 lift
  861. #if 1
  862. #define W_AM 3
  863. #define W_AO 0
  864. #define W_AS 1
  865. #define W_BM 1
  866. #define W_BO 8
  867. #define W_BS 4
  868. #undef lift5
  869. #define W_CM 9999
  870. #define W_CO 2
  871. #define W_CS 2
  872. #define W_DM 15
  873. #define W_DO 16
  874. #define W_DS 5
  875. #elif 0
  876. #define W_AM 55
  877. #define W_AO 16
  878. #define W_AS 5
  879. #define W_BM 3
  880. #define W_BO 32
  881. #define W_BS 6
  882. #define W_CM 127
  883. #define W_CO 64
  884. #define W_CS 7
  885. #define W_DM 7
  886. #define W_DO 8
  887. #define W_DS 4
  888. #elif 0
  889. #define W_AM 97
  890. #define W_AO 32
  891. #define W_AS 6
  892. #define W_BM 63
  893. #define W_BO 512
  894. #define W_BS 10
  895. #define W_CM 13
  896. #define W_CO 8
  897. #define W_CS 4
  898. #define W_DM 15
  899. #define W_DO 16
  900. #define W_DS 5
  901. #else
  902. #define W_AM 203
  903. #define W_AO 64
  904. #define W_AS 7
  905. #define W_BM 217
  906. #define W_BO 2048
  907. #define W_BS 12
  908. #define W_CM 113
  909. #define W_CO 64
  910. #define W_CS 7
  911. #define W_DM 227
  912. #define W_DO 128
  913. #define W_DS 9
  914. #endif
  915. static void horizontal_decompose97i(int *b, int width){
  916. int temp[width];
  917. const int w2= (width+1)>>1;
  918. lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
  919. lift (temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
  920. lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  921. lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  922. }
  923. static void vertical_decompose97iH0(int *b0, int *b1, int *b2, int width){
  924. int i;
  925. for(i=0; i<width; i++){
  926. b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  927. }
  928. }
  929. static void vertical_decompose97iH1(int *b0, int *b1, int *b2, int width){
  930. int i;
  931. for(i=0; i<width; i++){
  932. #ifdef lift5
  933. b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  934. #else
  935. int r= 3*(b0[i] + b2[i]);
  936. r+= r>>4;
  937. r+= r>>8;
  938. b1[i] += (r+W_CO)>>W_CS;
  939. #endif
  940. }
  941. }
  942. static void vertical_decompose97iL0(int *b0, int *b1, int *b2, int width){
  943. int i;
  944. for(i=0; i<width; i++){
  945. b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  946. }
  947. }
  948. static void vertical_decompose97iL1(int *b0, int *b1, int *b2, int width){
  949. int i;
  950. for(i=0; i<width; i++){
  951. b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  952. }
  953. }
  954. static void spatial_decompose97i(int *buffer, int width, int height, int stride){
  955. int y;
  956. DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
  957. DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
  958. DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
  959. DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
  960. for(y=-4; y<height; y+=2){
  961. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  962. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  963. {START_TIMER
  964. if(b3 <= b5) horizontal_decompose97i(b4, width);
  965. if(y+4 < height) horizontal_decompose97i(b5, width);
  966. if(width>400){
  967. STOP_TIMER("horizontal_decompose97i")
  968. }}
  969. {START_TIMER
  970. if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
  971. if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
  972. if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
  973. if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
  974. if(width>400){
  975. STOP_TIMER("vertical_decompose97i")
  976. }}
  977. b0=b2;
  978. b1=b3;
  979. b2=b4;
  980. b3=b5;
  981. }
  982. }
  983. void ff_spatial_dwt(int *buffer, int width, int height, int stride, int type, int decomposition_count){
  984. int level;
  985. for(level=0; level<decomposition_count; level++){
  986. switch(type){
  987. case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
  988. case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
  989. case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
  990. }
  991. }
  992. }
  993. static void horizontal_compose53i(int *b, int width){
  994. int temp[width];
  995. const int width2= width>>1;
  996. const int w2= (width+1)>>1;
  997. int A1,A2,A3,A4, x;
  998. #if 0
  999. A2= temp[1 ];
  1000. A4= temp[0 ];
  1001. A1= temp[0+width2];
  1002. A1 -= (A2 + A4)>>1;
  1003. A4 += (A1 + 1)>>1;
  1004. b[0+width2] = A1;
  1005. b[0 ] = A4;
  1006. for(x=1; x+1<width2; x+=2){
  1007. A3= temp[x+width2];
  1008. A4= temp[x+1 ];
  1009. A3 -= (A2 + A4)>>1;
  1010. A2 += (A1 + A3 + 2)>>2;
  1011. b[x+width2] = A3;
  1012. b[x ] = A2;
  1013. A1= temp[x+1+width2];
  1014. A2= temp[x+2 ];
  1015. A1 -= (A2 + A4)>>1;
  1016. A4 += (A1 + A3 + 2)>>2;
  1017. b[x+1+width2] = A1;
  1018. b[x+1 ] = A4;
  1019. }
  1020. A3= temp[width-1];
  1021. A3 -= A2;
  1022. A2 += (A1 + A3 + 2)>>2;
  1023. b[width -1] = A3;
  1024. b[width2-1] = A2;
  1025. #else
  1026. lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
  1027. lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
  1028. #endif
  1029. for(x=0; x<width2; x++){
  1030. b[2*x ]= temp[x ];
  1031. b[2*x + 1]= temp[x+w2];
  1032. }
  1033. if(width&1)
  1034. b[2*x ]= temp[x ];
  1035. }
  1036. static void vertical_compose53iH0(int *b0, int *b1, int *b2, int width){
  1037. int i;
  1038. for(i=0; i<width; i++){
  1039. b1[i] += (b0[i] + b2[i])>>1;
  1040. }
  1041. }
  1042. static void vertical_compose53iL0(int *b0, int *b1, int *b2, int width){
  1043. int i;
  1044. for(i=0; i<width; i++){
  1045. b1[i] -= (b0[i] + b2[i] + 2)>>2;
  1046. }
  1047. }
  1048. static void spatial_compose53i(int *buffer, int width, int height, int stride){
  1049. int y;
  1050. DWTELEM *b0= buffer + mirror(-1-1, height-1)*stride;
  1051. DWTELEM *b1= buffer + mirror(-1 , height-1)*stride;
  1052. for(y=-1; y<=height; y+=2){
  1053. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  1054. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  1055. {START_TIMER
  1056. if(b1 <= b3) vertical_compose53iL0(b1, b2, b3, width);
  1057. if(b0 <= b2) vertical_compose53iH0(b0, b1, b2, width);
  1058. STOP_TIMER("vertical_compose53i*")}
  1059. {START_TIMER
  1060. if(y-1 >= 0) horizontal_compose53i(b0, width);
  1061. if(b0 <= b2) horizontal_compose53i(b1, width);
  1062. STOP_TIMER("horizontal_compose53i")}
  1063. b0=b2;
  1064. b1=b3;
  1065. }
  1066. }
  1067. static void horizontal_compose97i(int *b, int width){
  1068. int temp[width];
  1069. const int w2= (width+1)>>1;
  1070. lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  1071. lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
  1072. lift (b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
  1073. lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
  1074. }
  1075. static void vertical_compose97iH0(int *b0, int *b1, int *b2, int width){
  1076. int i;
  1077. for(i=0; i<width; i++){
  1078. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  1079. }
  1080. }
  1081. static void vertical_compose97iH1(int *b0, int *b1, int *b2, int width){
  1082. int i;
  1083. for(i=0; i<width; i++){
  1084. #ifdef lift5
  1085. b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  1086. #else
  1087. int r= 3*(b0[i] + b2[i]);
  1088. r+= r>>4;
  1089. r+= r>>8;
  1090. b1[i] -= (r+W_CO)>>W_CS;
  1091. #endif
  1092. }
  1093. }
  1094. static void vertical_compose97iL0(int *b0, int *b1, int *b2, int width){
  1095. int i;
  1096. for(i=0; i<width; i++){
  1097. b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  1098. }
  1099. }
  1100. static void vertical_compose97iL1(int *b0, int *b1, int *b2, int width){
  1101. int i;
  1102. for(i=0; i<width; i++){
  1103. b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  1104. }
  1105. }
  1106. static void spatial_compose97i(int *buffer, int width, int height, int stride){
  1107. int y;
  1108. DWTELEM *b0= buffer + mirror(-3-1, height-1)*stride;
  1109. DWTELEM *b1= buffer + mirror(-3 , height-1)*stride;
  1110. DWTELEM *b2= buffer + mirror(-3+1, height-1)*stride;
  1111. DWTELEM *b3= buffer + mirror(-3+2, height-1)*stride;
  1112. for(y=-3; y<=height; y+=2){
  1113. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  1114. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  1115. if(stride == width && y+4 < height && 0){
  1116. int x;
  1117. for(x=0; x<width/2; x++)
  1118. b5[x] += 64*2;
  1119. for(; x<width; x++)
  1120. b5[x] += 169*2;
  1121. }
  1122. {START_TIMER
  1123. if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
  1124. if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
  1125. if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
  1126. if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
  1127. if(width>400){
  1128. STOP_TIMER("vertical_compose97i")}}
  1129. {START_TIMER
  1130. if(y-1>= 0) horizontal_compose97i(b0, width);
  1131. if(b0 <= b2) horizontal_compose97i(b1, width);
  1132. if(width>400 && b0 <= b2){
  1133. STOP_TIMER("horizontal_compose97i")}}
  1134. b0=b2;
  1135. b1=b3;
  1136. b2=b4;
  1137. b3=b5;
  1138. }
  1139. }
  1140. void ff_spatial_idwt(int *buffer, int width, int height, int stride, int type, int decomposition_count){
  1141. int level;
  1142. for(level=decomposition_count-1; level>=0; level--){
  1143. switch(type){
  1144. case 0: spatial_compose97i(buffer, width>>level, height>>level, stride<<level); break;
  1145. case 1: spatial_compose53i(buffer, width>>level, height>>level, stride<<level); break;
  1146. case 2: spatial_composeX (buffer, width>>level, height>>level, stride<<level); break;
  1147. }
  1148. }
  1149. }
  1150. static void encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1151. const int w= b->width;
  1152. const int h= b->height;
  1153. int x, y;
  1154. if(1){
  1155. int run=0;
  1156. int runs[w*h];
  1157. int run_index=0;
  1158. for(y=0; y<h; y++){
  1159. for(x=0; x<w; x++){
  1160. int v, p=0;
  1161. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1162. v= src[x + y*stride];
  1163. if(y){
  1164. t= src[x + (y-1)*stride];
  1165. if(x){
  1166. lt= src[x - 1 + (y-1)*stride];
  1167. }
  1168. if(x + 1 < w){
  1169. rt= src[x + 1 + (y-1)*stride];
  1170. }
  1171. }
  1172. if(x){
  1173. l= src[x - 1 + y*stride];
  1174. /*if(x > 1){
  1175. if(orientation==1) ll= src[y + (x-2)*stride];
  1176. else ll= src[x - 2 + y*stride];
  1177. }*/
  1178. }
  1179. if(parent){
  1180. int px= x>>1;
  1181. int py= y>>1;
  1182. if(px<b->parent->width && py<b->parent->height)
  1183. p= parent[px + py*2*stride];
  1184. }
  1185. if(!(/*ll|*/l|lt|t|rt|p)){
  1186. if(v){
  1187. runs[run_index++]= run;
  1188. run=0;
  1189. }else{
  1190. run++;
  1191. }
  1192. }
  1193. }
  1194. }
  1195. runs[run_index++]= run;
  1196. run_index=0;
  1197. run= runs[run_index++];
  1198. put_symbol2(&s->c, b->state[1], run, 3);
  1199. for(y=0; y<h; y++){
  1200. for(x=0; x<w; x++){
  1201. int v, p=0;
  1202. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1203. v= src[x + y*stride];
  1204. if(y){
  1205. t= src[x + (y-1)*stride];
  1206. if(x){
  1207. lt= src[x - 1 + (y-1)*stride];
  1208. }
  1209. if(x + 1 < w){
  1210. rt= src[x + 1 + (y-1)*stride];
  1211. }
  1212. }
  1213. if(x){
  1214. l= src[x - 1 + y*stride];
  1215. /*if(x > 1){
  1216. if(orientation==1) ll= src[y + (x-2)*stride];
  1217. else ll= src[x - 2 + y*stride];
  1218. }*/
  1219. }
  1220. if(parent){
  1221. int px= x>>1;
  1222. int py= y>>1;
  1223. if(px<b->parent->width && py<b->parent->height)
  1224. p= parent[px + py*2*stride];
  1225. }
  1226. if(/*ll|*/l|lt|t|rt|p){
  1227. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1228. put_cabac(&s->c, &b->state[0][context], !!v);
  1229. }else{
  1230. if(!run){
  1231. run= runs[run_index++];
  1232. put_symbol2(&s->c, b->state[1], run, 3);
  1233. assert(v);
  1234. }else{
  1235. run--;
  1236. assert(!v);
  1237. }
  1238. }
  1239. if(v){
  1240. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1241. put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
  1242. put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
  1243. }
  1244. }
  1245. }
  1246. }
  1247. }
  1248. static void encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1249. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  1250. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  1251. encode_subband_c0run(s, b, src, parent, stride, orientation);
  1252. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  1253. }
  1254. static inline void decode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1255. const int w= b->width;
  1256. const int h= b->height;
  1257. int x,y;
  1258. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  1259. int qmul= qexp[qlog&7]<<(qlog>>3);
  1260. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1261. START_TIMER
  1262. if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){
  1263. qadd= 0;
  1264. qmul= 1<<QEXPSHIFT;
  1265. }
  1266. if(1){
  1267. int run;
  1268. int index=0;
  1269. int prev_index=-1;
  1270. int prev2_index=0;
  1271. int parent_index= 0;
  1272. int prev_parent_index= 0;
  1273. for(y=0; y<b->height; y++)
  1274. memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
  1275. run= get_symbol2(&s->c, b->state[1], 3);
  1276. for(y=0; y<h; y++){
  1277. int v=0;
  1278. int lt=0, t=0, rt=0;
  1279. if(y && b->x[prev_index] == 0){
  1280. rt= b->coeff[prev_index];
  1281. }
  1282. for(x=0; x<w; x++){
  1283. int p=0;
  1284. const int l= v;
  1285. lt= t; t= rt;
  1286. if(y){
  1287. if(b->x[prev_index] <= x)
  1288. prev_index++;
  1289. if(b->x[prev_index] == x + 1)
  1290. rt= b->coeff[prev_index];
  1291. else
  1292. rt=0;
  1293. }
  1294. if(parent){
  1295. if(x>>1 > b->parent->x[parent_index]){
  1296. parent_index++;
  1297. }
  1298. if(x>>1 == b->parent->x[parent_index]){
  1299. p= b->parent->coeff[parent_index];
  1300. }
  1301. }
  1302. if(/*ll|*/l|lt|t|rt|p){
  1303. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1304. v=get_cabac(&s->c, &b->state[0][context]);
  1305. }else{
  1306. if(!run){
  1307. run= get_symbol2(&s->c, b->state[1], 3);
  1308. v=1;
  1309. }else{
  1310. run--;
  1311. v=0;
  1312. if(y && parent){
  1313. int max_run;
  1314. max_run= FFMIN(run, b->x[prev_index] - x - 2);
  1315. max_run= FFMIN(max_run, 2*b->parent->x[parent_index] - x - 1);
  1316. x+= max_run;
  1317. run-= max_run;
  1318. }
  1319. }
  1320. }
  1321. if(v){
  1322. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1323. v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1;
  1324. if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]])){
  1325. src[x + y*stride]=-(( v*qmul + qadd)>>(QEXPSHIFT));
  1326. v= -v;
  1327. }else{
  1328. src[x + y*stride]= (( v*qmul + qadd)>>(QEXPSHIFT));
  1329. }
  1330. b->x[index]=x; //FIXME interleave x/coeff
  1331. b->coeff[index++]= v;
  1332. }
  1333. }
  1334. b->x[index++]= w+1; //end marker
  1335. prev_index= prev2_index;
  1336. prev2_index= index;
  1337. if(parent){
  1338. while(b->parent->x[parent_index] != b->parent->width+1)
  1339. parent_index++;
  1340. parent_index++;
  1341. if(y&1){
  1342. prev_parent_index= parent_index;
  1343. }else{
  1344. parent_index= prev_parent_index;
  1345. }
  1346. }
  1347. }
  1348. b->x[index++]= w+1; //end marker
  1349. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  1350. STOP_TIMER("decode_subband")
  1351. }
  1352. return;
  1353. }
  1354. }
  1355. static void reset_contexts(SnowContext *s){
  1356. int plane_index, level, orientation;
  1357. for(plane_index=0; plane_index<2; plane_index++){
  1358. for(level=0; level<s->spatial_decomposition_count; level++){
  1359. for(orientation=level ? 1:0; orientation<4; orientation++){
  1360. memset(s->plane[plane_index].band[level][orientation].state, 0, sizeof(s->plane[plane_index].band[level][orientation].state));
  1361. }
  1362. }
  1363. }
  1364. memset(s->header_state, 0, sizeof(s->header_state));
  1365. memset(s->block_state, 0, sizeof(s->block_state));
  1366. }
  1367. static int alloc_blocks(SnowContext *s){
  1368. int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
  1369. int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
  1370. s->b_width = w;
  1371. s->b_height= h;
  1372. s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
  1373. return 0;
  1374. }
  1375. static inline void copy_cabac_state(CABACContext *d, CABACContext *s){
  1376. PutBitContext bak= d->pb;
  1377. *d= *s;
  1378. d->pb= bak;
  1379. }
  1380. //near copy & paste from dsputil, FIXME
  1381. static int pix_sum(uint8_t * pix, int line_size, int w)
  1382. {
  1383. int s, i, j;
  1384. s = 0;
  1385. for (i = 0; i < w; i++) {
  1386. for (j = 0; j < w; j++) {
  1387. s += pix[0];
  1388. pix ++;
  1389. }
  1390. pix += line_size - w;
  1391. }
  1392. return s;
  1393. }
  1394. //near copy & paste from dsputil, FIXME
  1395. static int pix_norm1(uint8_t * pix, int line_size, int w)
  1396. {
  1397. int s, i, j;
  1398. uint32_t *sq = squareTbl + 256;
  1399. s = 0;
  1400. for (i = 0; i < w; i++) {
  1401. for (j = 0; j < w; j ++) {
  1402. s += sq[pix[0]];
  1403. pix ++;
  1404. }
  1405. pix += line_size - w;
  1406. }
  1407. return s;
  1408. }
  1409. static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int type){
  1410. const int w= s->b_width << s->block_max_depth;
  1411. const int rem_depth= s->block_max_depth - level;
  1412. const int index= (x + y*w) << rem_depth;
  1413. const int block_w= 1<<rem_depth;
  1414. BlockNode block;
  1415. int i,j;
  1416. block.color[0]= l;
  1417. block.color[1]= cb;
  1418. block.color[2]= cr;
  1419. block.mx= mx;
  1420. block.my= my;
  1421. block.type= type;
  1422. block.level= level;
  1423. for(j=0; j<block_w; j++){
  1424. for(i=0; i<block_w; i++){
  1425. s->block[index + i + j*w]= block;
  1426. }
  1427. }
  1428. }
  1429. static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
  1430. const int offset[3]= {
  1431. y*c-> stride + x,
  1432. ((y*c->uvstride + x)>>1),
  1433. ((y*c->uvstride + x)>>1),
  1434. };
  1435. int i;
  1436. for(i=0; i<3; i++){
  1437. c->src[0][i]= src [i];
  1438. c->ref[0][i]= ref [i] + offset[i];
  1439. }
  1440. assert(!ref_index);
  1441. }
  1442. //FIXME copy&paste
  1443. #define P_LEFT P[1]
  1444. #define P_TOP P[2]
  1445. #define P_TOPRIGHT P[3]
  1446. #define P_MEDIAN P[4]
  1447. #define P_MV1 P[9]
  1448. #define FLAG_QPEL 1 //must be 1
  1449. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  1450. uint8_t p_buffer[1024];
  1451. uint8_t i_buffer[1024];
  1452. uint8_t p_state[sizeof(s->block_state)];
  1453. uint8_t i_state[sizeof(s->block_state)];
  1454. CABACContext pc, ic;
  1455. PutBitContext pbbak= s->c.pb;
  1456. int score, score2, iscore, i_len, p_len, block_s, sum;
  1457. const int w= s->b_width << s->block_max_depth;
  1458. const int h= s->b_height << s->block_max_depth;
  1459. const int rem_depth= s->block_max_depth - level;
  1460. const int index= (x + y*w) << rem_depth;
  1461. const int block_w= 1<<(LOG2_MB_SIZE - level);
  1462. static BlockNode null_block= { //FIXME add border maybe
  1463. .color= {128,128,128},
  1464. .mx= 0,
  1465. .my= 0,
  1466. .type= 0,
  1467. .level= 0,
  1468. };
  1469. int trx= (x+1)<<rem_depth;
  1470. int try= (y+1)<<rem_depth;
  1471. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1472. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1473. BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  1474. BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  1475. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1476. BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1477. int pl = left->color[0];
  1478. int pcb= left->color[1];
  1479. int pcr= left->color[2];
  1480. int pmx= mid_pred(left->mx, top->mx, tr->mx);
  1481. int pmy= mid_pred(left->my, top->my, tr->my);
  1482. int mx=0, my=0;
  1483. int l,cr,cb, i;
  1484. const int stride= s->current_picture.linesize[0];
  1485. const int uvstride= s->current_picture.linesize[1];
  1486. const int instride= s->input_picture.linesize[0];
  1487. const int uvinstride= s->input_picture.linesize[1];
  1488. uint8_t *new_l = s->input_picture.data[0] + (x + y* instride)*block_w;
  1489. uint8_t *new_cb= s->input_picture.data[1] + (x + y*uvinstride)*block_w/2;
  1490. uint8_t *new_cr= s->input_picture.data[2] + (x + y*uvinstride)*block_w/2;
  1491. uint8_t current_mb[3][stride*block_w];
  1492. uint8_t *current_data[3]= {&current_mb[0][0], &current_mb[1][0], &current_mb[2][0]};
  1493. int P[10][2];
  1494. int16_t last_mv[3][2];
  1495. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  1496. const int shift= 1+qpel;
  1497. MotionEstContext *c= &s->m.me;
  1498. int mx_context= av_log2(2*ABS(left->mx - top->mx));
  1499. int my_context= av_log2(2*ABS(left->my - top->my));
  1500. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1501. assert(sizeof(s->block_state) >= 256);
  1502. if(s->keyframe){
  1503. set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, BLOCK_INTRA);
  1504. return 0;
  1505. }
  1506. //FIXME optimize
  1507. for(i=0; i<block_w; i++)
  1508. memcpy(&current_mb[0][0] + stride*i, new_l + instride*i, block_w);
  1509. for(i=0; i<block_w>>1; i++)
  1510. memcpy(&current_mb[1][0] + uvstride*i, new_cb + uvinstride*i, block_w>>1);
  1511. for(i=0; i<block_w>>1; i++)
  1512. memcpy(&current_mb[2][0] + uvstride*i, new_cr + uvinstride*i, block_w>>1);
  1513. // clip predictors / edge ?
  1514. P_LEFT[0]= left->mx;
  1515. P_LEFT[1]= left->my;
  1516. P_TOP [0]= top->mx;
  1517. P_TOP [1]= top->my;
  1518. P_TOPRIGHT[0]= tr->mx;
  1519. P_TOPRIGHT[1]= tr->my;
  1520. last_mv[0][0]= s->block[index].mx;
  1521. last_mv[0][1]= s->block[index].my;
  1522. last_mv[1][0]= right->mx;
  1523. last_mv[1][1]= right->my;
  1524. last_mv[2][0]= bottom->mx;
  1525. last_mv[2][1]= bottom->my;
  1526. s->m.mb_stride=2;
  1527. s->m.mb_x=
  1528. s->m.mb_y= 0;
  1529. s->m.me.skip= 0;
  1530. init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0);
  1531. assert(s->m.me. stride == stride);
  1532. assert(s->m.me.uvstride == uvstride);
  1533. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1534. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1535. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1536. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  1537. c->xmin = - x*block_w - 16;
  1538. c->ymin = - y*block_w - 16;
  1539. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16;
  1540. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16;
  1541. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1542. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  1543. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  1544. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1545. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1546. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  1547. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1548. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1549. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1550. if (!y) {
  1551. c->pred_x= P_LEFT[0];
  1552. c->pred_y= P_LEFT[1];
  1553. } else {
  1554. c->pred_x = P_MEDIAN[0];
  1555. c->pred_y = P_MEDIAN[1];
  1556. }
  1557. score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, /*ref_index*/ 0, last_mv,
  1558. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  1559. assert(mx >= c->xmin);
  1560. assert(mx <= c->xmax);
  1561. assert(my >= c->ymin);
  1562. assert(my <= c->ymax);
  1563. score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  1564. score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  1565. //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2
  1566. // subpel search
  1567. pc= s->c;
  1568. init_put_bits(&pc.pb, p_buffer, sizeof(p_buffer));
  1569. memcpy(p_state, s->block_state, sizeof(s->block_state));
  1570. if(level!=s->block_max_depth)
  1571. put_cabac(&pc, &p_state[4 + s_context], 1);
  1572. put_cabac(&pc, &p_state[1 + left->type + top->type], 0);
  1573. put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1);
  1574. put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1);
  1575. p_len= put_bits_count(&pc.pb);
  1576. score += (s->lambda2*(p_len + pc.outstanding_count - s->c.outstanding_count))>>FF_LAMBDA_SHIFT;
  1577. block_s= block_w*block_w;
  1578. sum = pix_sum(&current_mb[0][0], stride, block_w);
  1579. l= (sum + block_s/2)/block_s;
  1580. iscore = pix_norm1(&current_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s;
  1581. block_s= block_w*block_w>>2;
  1582. sum = pix_sum(&current_mb[1][0], uvstride, block_w>>1);
  1583. cb= (sum + block_s/2)/block_s;
  1584. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  1585. sum = pix_sum(&current_mb[2][0], uvstride, block_w>>1);
  1586. cr= (sum + block_s/2)/block_s;
  1587. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  1588. ic= s->c;
  1589. init_put_bits(&ic.pb, i_buffer, sizeof(i_buffer));
  1590. memcpy(i_state, s->block_state, sizeof(s->block_state));
  1591. if(level!=s->block_max_depth)
  1592. put_cabac(&ic, &i_state[4 + s_context], 1);
  1593. put_cabac(&ic, &i_state[1 + left->type + top->type], 1);
  1594. put_symbol(&ic, &i_state[32], l-pl , 1);
  1595. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  1596. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  1597. i_len= put_bits_count(&ic.pb);
  1598. iscore += (s->lambda2*(i_len + ic.outstanding_count - s->c.outstanding_count))>>FF_LAMBDA_SHIFT;
  1599. // assert(score==256*256*256*64-1);
  1600. assert(iscore < 255*255*256 + s->lambda2*10);
  1601. assert(iscore >= 0);
  1602. assert(l>=0 && l<=255);
  1603. assert(pl>=0 && pl<=255);
  1604. if(level==0){
  1605. int varc= iscore >> 8;
  1606. int vard= score >> 8;
  1607. if (vard <= 64 || vard < varc)
  1608. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1609. else
  1610. c->scene_change_score+= s->m.qscale;
  1611. }
  1612. if(level!=s->block_max_depth){
  1613. put_cabac(&s->c, &s->block_state[4 + s_context], 0);
  1614. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1615. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1616. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1617. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1618. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  1619. if(score2 < score && score2 < iscore)
  1620. return score2;
  1621. }
  1622. if(iscore < score){
  1623. flush_put_bits(&ic.pb);
  1624. ff_copy_bits(&pbbak, i_buffer, i_len);
  1625. s->c= ic;
  1626. s->c.pb= pbbak;
  1627. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA);
  1628. memcpy(s->block_state, i_state, sizeof(s->block_state));
  1629. return iscore;
  1630. }else{
  1631. flush_put_bits(&pc.pb);
  1632. ff_copy_bits(&pbbak, p_buffer, p_len);
  1633. s->c= pc;
  1634. s->c.pb= pbbak;
  1635. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0);
  1636. memcpy(s->block_state, p_state, sizeof(s->block_state));
  1637. return score;
  1638. }
  1639. }
  1640. static void decode_q_branch(SnowContext *s, int level, int x, int y){
  1641. const int w= s->b_width << s->block_max_depth;
  1642. const int rem_depth= s->block_max_depth - level;
  1643. const int index= (x + y*w) << rem_depth;
  1644. static BlockNode null_block= { //FIXME add border maybe
  1645. .color= {128,128,128},
  1646. .mx= 0,
  1647. .my= 0,
  1648. .type= 0,
  1649. .level= 0,
  1650. };
  1651. int trx= (x+1)<<rem_depth;
  1652. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1653. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1654. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1655. BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1656. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1657. if(s->keyframe){
  1658. set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, BLOCK_INTRA);
  1659. return;
  1660. }
  1661. if(level==s->block_max_depth || get_cabac(&s->c, &s->block_state[4 + s_context])){
  1662. int type;
  1663. int l = left->color[0];
  1664. int cb= left->color[1];
  1665. int cr= left->color[2];
  1666. int mx= mid_pred(left->mx, top->mx, tr->mx);
  1667. int my= mid_pred(left->my, top->my, tr->my);
  1668. int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx));
  1669. int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my));
  1670. type= get_cabac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  1671. if(type){
  1672. l += get_symbol(&s->c, &s->block_state[32], 1);
  1673. cb+= get_symbol(&s->c, &s->block_state[64], 1);
  1674. cr+= get_symbol(&s->c, &s->block_state[96], 1);
  1675. }else{
  1676. mx+= get_symbol(&s->c, &s->block_state[128 + 32*mx_context], 1);
  1677. my+= get_symbol(&s->c, &s->block_state[128 + 32*my_context], 1);
  1678. }
  1679. set_blocks(s, level, x, y, l, cb, cr, mx, my, type);
  1680. }else{
  1681. decode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1682. decode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1683. decode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1684. decode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1685. }
  1686. }
  1687. static void encode_blocks(SnowContext *s){
  1688. int x, y;
  1689. int w= s->b_width;
  1690. int h= s->b_height;
  1691. for(y=0; y<h; y++){
  1692. for(x=0; x<w; x++){
  1693. encode_q_branch(s, 0, x, y);
  1694. }
  1695. }
  1696. }
  1697. static void decode_blocks(SnowContext *s){
  1698. int x, y;
  1699. int w= s->b_width;
  1700. int h= s->b_height;
  1701. for(y=0; y<h; y++){
  1702. for(x=0; x<w; x++){
  1703. decode_q_branch(s, 0, x, y);
  1704. }
  1705. }
  1706. }
  1707. static void mc_block(uint8_t *dst, uint8_t *src, uint8_t *tmp, int stride, int b_w, int b_h, int dx, int dy){
  1708. int x, y;
  1709. for(y=0; y < b_h+5; y++){
  1710. for(x=0; x < b_w; x++){
  1711. int a0= src[x + y*stride];
  1712. int a1= src[x + 1 + y*stride];
  1713. int a2= src[x + 2 + y*stride];
  1714. int a3= src[x + 3 + y*stride];
  1715. int a4= src[x + 4 + y*stride];
  1716. int a5= src[x + 5 + y*stride];
  1717. // int am= 9*(a1+a2) - (a0+a3);
  1718. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  1719. // int am= 18*(a2+a3) - 2*(a1+a4);
  1720. // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  1721. // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
  1722. // if(b_w==16) am= 8*(a1+a2);
  1723. if(dx<8) tmp[x + y*stride]= (32*a2*( 8-dx) + am* dx + 128)>>8;
  1724. else tmp[x + y*stride]= ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
  1725. /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
  1726. else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
  1727. else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
  1728. else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
  1729. }
  1730. }
  1731. for(y=0; y < b_h; y++){
  1732. for(x=0; x < b_w; x++){
  1733. int a0= tmp[x + y *stride];
  1734. int a1= tmp[x + (y + 1)*stride];
  1735. int a2= tmp[x + (y + 2)*stride];
  1736. int a3= tmp[x + (y + 3)*stride];
  1737. int a4= tmp[x + (y + 4)*stride];
  1738. int a5= tmp[x + (y + 5)*stride];
  1739. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  1740. // int am= 18*(a2+a3) - 2*(a1+a4);
  1741. /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  1742. int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
  1743. // if(b_w==16) am= 8*(a1+a2);
  1744. if(dy<8) dst[x + y*stride]= (32*a2*( 8-dy) + am* dy + 128)>>8;
  1745. else dst[x + y*stride]= ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
  1746. /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
  1747. else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
  1748. else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
  1749. else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
  1750. }
  1751. }
  1752. }
  1753. #define mcb(dx,dy,b_w)\
  1754. static void mc_block ## dx ## dy(uint8_t *dst, uint8_t *src, int stride){\
  1755. uint8_t tmp[stride*(b_w+5)];\
  1756. mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
  1757. }
  1758. mcb( 0, 0,16)
  1759. mcb( 4, 0,16)
  1760. mcb( 8, 0,16)
  1761. mcb(12, 0,16)
  1762. mcb( 0, 4,16)
  1763. mcb( 4, 4,16)
  1764. mcb( 8, 4,16)
  1765. mcb(12, 4,16)
  1766. mcb( 0, 8,16)
  1767. mcb( 4, 8,16)
  1768. mcb( 8, 8,16)
  1769. mcb(12, 8,16)
  1770. mcb( 0,12,16)
  1771. mcb( 4,12,16)
  1772. mcb( 8,12,16)
  1773. mcb(12,12,16)
  1774. #define mca(dx,dy,b_w)\
  1775. static void mc_block_hpel ## dx ## dy(uint8_t *dst, uint8_t *src, int stride, int h){\
  1776. uint8_t tmp[stride*(b_w+5)];\
  1777. assert(h==b_w);\
  1778. mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
  1779. }
  1780. mca( 0, 0,16)
  1781. mca( 8, 0,16)
  1782. mca( 0, 8,16)
  1783. mca( 8, 8,16)
  1784. static always_inline void add_xblock(SnowContext *s, DWTELEM *dst, uint8_t *src, uint8_t *obmc, int s_x, int s_y, int b_w, int b_h, int mv_x, int mv_y, int w, int h, int dst_stride, int src_stride, int obmc_stride, int mb_type, int add, int color){
  1785. uint8_t tmp[src_stride*(b_h+5)]; //FIXME move to context to gurantee alignment
  1786. int x,y;
  1787. if(s_x<0){
  1788. obmc -= s_x;
  1789. b_w += s_x;
  1790. s_x=0;
  1791. }else if(s_x + b_w > w){
  1792. b_w = w - s_x;
  1793. }
  1794. if(s_y<0){
  1795. obmc -= s_y*obmc_stride;
  1796. b_h += s_y;
  1797. s_y=0;
  1798. }else if(s_y + b_h> h){
  1799. b_h = h - s_y;
  1800. }
  1801. if(b_w<=0 || b_h<=0) return;
  1802. dst += s_x + s_y*dst_stride;
  1803. if(mb_type==BLOCK_INTRA){
  1804. for(y=0; y < b_h; y++){
  1805. for(x=0; x < b_w; x++){
  1806. if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * color * (256/OBMC_MAX);
  1807. else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * color * (256/OBMC_MAX);
  1808. }
  1809. }
  1810. }else{
  1811. int dx= mv_x&15;
  1812. int dy= mv_y&15;
  1813. // int dxy= (mv_x&1) + 2*(mv_y&1);
  1814. s_x += (mv_x>>4) - 2;
  1815. s_y += (mv_y>>4) - 2;
  1816. src += s_x + s_y*src_stride;
  1817. //use dsputil
  1818. if( (unsigned)s_x >= w - b_w - 4
  1819. || (unsigned)s_y >= h - b_h - 4){
  1820. ff_emulated_edge_mc(tmp + 32, src, src_stride, b_w+5, b_h+5, s_x, s_y, w, h);
  1821. src= tmp + 32;
  1822. }
  1823. assert(mb_type==0);
  1824. mc_block(tmp, src, tmp + 64+8, src_stride, b_w, b_h, dx, dy);
  1825. for(y=0; y < b_h; y++){
  1826. for(x=0; x < b_w; x++){
  1827. if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
  1828. else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
  1829. }
  1830. }
  1831. }
  1832. }
  1833. static void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
  1834. Plane *p= &s->plane[plane_index];
  1835. const int mb_w= s->b_width << s->block_max_depth;
  1836. const int mb_h= s->b_height << s->block_max_depth;
  1837. const int mb_stride= mb_w;
  1838. int x, y, mb_x, mb_y;
  1839. int scale = plane_index ? s->mv_scale : 2*s->mv_scale;
  1840. int block_size = MB_SIZE >> s->block_max_depth;
  1841. int block_w = plane_index ? block_size/2 : block_size;
  1842. uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  1843. int obmc_stride= plane_index ? block_size : 2*block_size;
  1844. int ref_stride= s->last_picture.linesize[plane_index];
  1845. uint8_t *ref = s->last_picture.data[plane_index];
  1846. int w= p->width;
  1847. int h= p->height;
  1848. START_TIMER
  1849. if(s->avctx->debug&512){
  1850. for(y=0; y<h; y++){
  1851. for(x=0; x<w; x++){
  1852. if(add) buf[x + y*w]+= 128*256;
  1853. else buf[x + y*w]-= 128*256;
  1854. }
  1855. }
  1856. return;
  1857. }
  1858. for(mb_y=-1; mb_y<=mb_h; mb_y++){
  1859. for(mb_x=-1; mb_x<=mb_w; mb_x++){
  1860. int index= clip(mb_x, 0, mb_w-1) + clip(mb_y, 0, mb_h-1)*mb_stride;
  1861. START_TIMER
  1862. add_xblock(s, buf, ref, obmc,
  1863. block_w*mb_x - block_w/2,
  1864. block_w*mb_y - block_w/2,
  1865. 2*block_w, 2*block_w,
  1866. s->block[index].mx*scale, s->block[index].my*scale,
  1867. w, h,
  1868. w, ref_stride, obmc_stride,
  1869. s->block[index].type, add, s->block[index].color[plane_index]);
  1870. STOP_TIMER("add_xblock")
  1871. }
  1872. }
  1873. STOP_TIMER("predict_plane")
  1874. }
  1875. static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
  1876. const int level= b->level;
  1877. const int w= b->width;
  1878. const int h= b->height;
  1879. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  1880. const int qmul= qexp[qlog&7]<<(qlog>>3);
  1881. int x,y, thres1, thres2;
  1882. START_TIMER
  1883. assert(QROOT==8);
  1884. if(s->qlog == LOSSLESS_QLOG) return;
  1885. bias= bias ? 0 : (3*qmul)>>3;
  1886. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  1887. thres2= 2*thres1;
  1888. if(!bias){
  1889. for(y=0; y<h; y++){
  1890. for(x=0; x<w; x++){
  1891. int i= src[x + y*stride];
  1892. if((unsigned)(i+thres1) > thres2){
  1893. if(i>=0){
  1894. i<<= QEXPSHIFT;
  1895. i/= qmul; //FIXME optimize
  1896. src[x + y*stride]= i;
  1897. }else{
  1898. i= -i;
  1899. i<<= QEXPSHIFT;
  1900. i/= qmul; //FIXME optimize
  1901. src[x + y*stride]= -i;
  1902. }
  1903. }else
  1904. src[x + y*stride]= 0;
  1905. }
  1906. }
  1907. }else{
  1908. for(y=0; y<h; y++){
  1909. for(x=0; x<w; x++){
  1910. int i= src[x + y*stride];
  1911. if((unsigned)(i+thres1) > thres2){
  1912. if(i>=0){
  1913. i<<= QEXPSHIFT;
  1914. i= (i + bias) / qmul; //FIXME optimize
  1915. src[x + y*stride]= i;
  1916. }else{
  1917. i= -i;
  1918. i<<= QEXPSHIFT;
  1919. i= (i + bias) / qmul; //FIXME optimize
  1920. src[x + y*stride]= -i;
  1921. }
  1922. }else
  1923. src[x + y*stride]= 0;
  1924. }
  1925. }
  1926. }
  1927. if(level+1 == s->spatial_decomposition_count){
  1928. // STOP_TIMER("quantize")
  1929. }
  1930. }
  1931. static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
  1932. const int w= b->width;
  1933. const int h= b->height;
  1934. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  1935. const int qmul= qexp[qlog&7]<<(qlog>>3);
  1936. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1937. int x,y;
  1938. START_TIMER
  1939. if(s->qlog == LOSSLESS_QLOG) return;
  1940. assert(QROOT==8);
  1941. for(y=0; y<h; y++){
  1942. for(x=0; x<w; x++){
  1943. int i= src[x + y*stride];
  1944. if(i<0){
  1945. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  1946. }else if(i>0){
  1947. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  1948. }
  1949. }
  1950. }
  1951. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  1952. STOP_TIMER("dquant")
  1953. }
  1954. }
  1955. static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  1956. const int w= b->width;
  1957. const int h= b->height;
  1958. int x,y;
  1959. for(y=h-1; y>=0; y--){
  1960. for(x=w-1; x>=0; x--){
  1961. int i= x + y*stride;
  1962. if(x){
  1963. if(use_median){
  1964. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  1965. else src[i] -= src[i - 1];
  1966. }else{
  1967. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  1968. else src[i] -= src[i - 1];
  1969. }
  1970. }else{
  1971. if(y) src[i] -= src[i - stride];
  1972. }
  1973. }
  1974. }
  1975. }
  1976. static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  1977. const int w= b->width;
  1978. const int h= b->height;
  1979. int x,y;
  1980. for(y=0; y<h; y++){
  1981. for(x=0; x<w; x++){
  1982. int i= x + y*stride;
  1983. if(x){
  1984. if(use_median){
  1985. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  1986. else src[i] += src[i - 1];
  1987. }else{
  1988. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  1989. else src[i] += src[i - 1];
  1990. }
  1991. }else{
  1992. if(y) src[i] += src[i - stride];
  1993. }
  1994. }
  1995. }
  1996. }
  1997. static void encode_header(SnowContext *s){
  1998. int plane_index, level, orientation;
  1999. put_cabac(&s->c, s->header_state, s->keyframe); // state clearing stuff?
  2000. if(s->keyframe){
  2001. put_symbol(&s->c, s->header_state, s->version, 0);
  2002. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  2003. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  2004. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2005. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  2006. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  2007. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  2008. put_cabac(&s->c, s->header_state, s->spatial_scalability);
  2009. // put_cabac(&s->c, s->header_state, s->rate_scalability);
  2010. for(plane_index=0; plane_index<2; plane_index++){
  2011. for(level=0; level<s->spatial_decomposition_count; level++){
  2012. for(orientation=level ? 1:0; orientation<4; orientation++){
  2013. if(orientation==2) continue;
  2014. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  2015. }
  2016. }
  2017. }
  2018. }
  2019. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
  2020. put_symbol(&s->c, s->header_state, s->qlog, 1);
  2021. put_symbol(&s->c, s->header_state, s->mv_scale, 0);
  2022. put_symbol(&s->c, s->header_state, s->qbias, 1);
  2023. put_symbol(&s->c, s->header_state, s->block_max_depth, 0);
  2024. }
  2025. static int decode_header(SnowContext *s){
  2026. int plane_index, level, orientation;
  2027. s->keyframe= get_cabac(&s->c, s->header_state);
  2028. if(s->keyframe){
  2029. s->version= get_symbol(&s->c, s->header_state, 0);
  2030. if(s->version>0){
  2031. av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
  2032. return -1;
  2033. }
  2034. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2035. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2036. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2037. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  2038. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  2039. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  2040. s->spatial_scalability= get_cabac(&s->c, s->header_state);
  2041. // s->rate_scalability= get_cabac(&s->c, s->header_state);
  2042. for(plane_index=0; plane_index<3; plane_index++){
  2043. for(level=0; level<s->spatial_decomposition_count; level++){
  2044. for(orientation=level ? 1:0; orientation<4; orientation++){
  2045. int q;
  2046. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  2047. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  2048. else q= get_symbol(&s->c, s->header_state, 1);
  2049. s->plane[plane_index].band[level][orientation].qlog= q;
  2050. }
  2051. }
  2052. }
  2053. }
  2054. s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2055. if(s->spatial_decomposition_type > 2){
  2056. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  2057. return -1;
  2058. }
  2059. s->qlog= get_symbol(&s->c, s->header_state, 1);
  2060. s->mv_scale= get_symbol(&s->c, s->header_state, 0);
  2061. s->qbias= get_symbol(&s->c, s->header_state, 1);
  2062. s->block_max_depth= get_symbol(&s->c, s->header_state, 0);
  2063. return 0;
  2064. }
  2065. static int common_init(AVCodecContext *avctx){
  2066. SnowContext *s = avctx->priv_data;
  2067. int width, height;
  2068. int level, orientation, plane_index, dec;
  2069. s->avctx= avctx;
  2070. dsputil_init(&s->dsp, avctx);
  2071. #define mcf(dx,dy)\
  2072. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  2073. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  2074. mc_block ## dx ## dy;
  2075. mcf( 0, 0)
  2076. mcf( 4, 0)
  2077. mcf( 8, 0)
  2078. mcf(12, 0)
  2079. mcf( 0, 4)
  2080. mcf( 4, 4)
  2081. mcf( 8, 4)
  2082. mcf(12, 4)
  2083. mcf( 0, 8)
  2084. mcf( 4, 8)
  2085. mcf( 8, 8)
  2086. mcf(12, 8)
  2087. mcf( 0,12)
  2088. mcf( 4,12)
  2089. mcf( 8,12)
  2090. mcf(12,12)
  2091. #define mcfh(dx,dy)\
  2092. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  2093. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  2094. mc_block_hpel ## dx ## dy;
  2095. mcfh(0, 0)
  2096. mcfh(8, 0)
  2097. mcfh(0, 8)
  2098. mcfh(8, 8)
  2099. dec= s->spatial_decomposition_count= 5;
  2100. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  2101. s->chroma_h_shift= 1; //FIXME XXX
  2102. s->chroma_v_shift= 1;
  2103. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  2104. width= s->avctx->width;
  2105. height= s->avctx->height;
  2106. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
  2107. s->pred_buffer= av_mallocz(width*height*sizeof(DWTELEM));
  2108. s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  2109. s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
  2110. for(plane_index=0; plane_index<3; plane_index++){
  2111. int w= s->avctx->width;
  2112. int h= s->avctx->height;
  2113. if(plane_index){
  2114. w>>= s->chroma_h_shift;
  2115. h>>= s->chroma_v_shift;
  2116. }
  2117. s->plane[plane_index].width = w;
  2118. s->plane[plane_index].height= h;
  2119. //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
  2120. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  2121. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2122. SubBand *b= &s->plane[plane_index].band[level][orientation];
  2123. b->buf= s->spatial_dwt_buffer;
  2124. b->level= level;
  2125. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  2126. b->width = (w + !(orientation&1))>>1;
  2127. b->height= (h + !(orientation>1))>>1;
  2128. if(orientation&1) b->buf += (w+1)>>1;
  2129. if(orientation>1) b->buf += b->stride>>1;
  2130. if(level)
  2131. b->parent= &s->plane[plane_index].band[level-1][orientation];
  2132. b->x = av_mallocz(((b->width+1) * b->height+1)*sizeof(int16_t));
  2133. b->coeff= av_mallocz(((b->width+1) * b->height+1)*sizeof(DWTELEM));
  2134. }
  2135. w= (w+1)>>1;
  2136. h= (h+1)>>1;
  2137. }
  2138. }
  2139. reset_contexts(s);
  2140. /*
  2141. width= s->width= avctx->width;
  2142. height= s->height= avctx->height;
  2143. assert(width && height);
  2144. */
  2145. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  2146. return 0;
  2147. }
  2148. static void calculate_vissual_weight(SnowContext *s, Plane *p){
  2149. int width = p->width;
  2150. int height= p->height;
  2151. int level, orientation, x, y;
  2152. for(level=0; level<s->spatial_decomposition_count; level++){
  2153. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2154. SubBand *b= &p->band[level][orientation];
  2155. DWTELEM *buf= b->buf;
  2156. int64_t error=0;
  2157. memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
  2158. buf[b->width/2 + b->height/2*b->stride]= 256*256;
  2159. ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2160. for(y=0; y<height; y++){
  2161. for(x=0; x<width; x++){
  2162. int64_t d= s->spatial_dwt_buffer[x + y*width];
  2163. error += d*d;
  2164. }
  2165. }
  2166. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  2167. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
  2168. }
  2169. }
  2170. }
  2171. static int encode_init(AVCodecContext *avctx)
  2172. {
  2173. SnowContext *s = avctx->priv_data;
  2174. int plane_index;
  2175. if(avctx->strict_std_compliance >= 0){
  2176. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  2177. "use vstrict=-1 to use it anyway\n");
  2178. return -1;
  2179. }
  2180. common_init(avctx);
  2181. alloc_blocks(s);
  2182. s->version=0;
  2183. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  2184. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2185. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2186. h263_encode_init(&s->m); //mv_penalty
  2187. for(plane_index=0; plane_index<3; plane_index++){
  2188. calculate_vissual_weight(s, &s->plane[plane_index]);
  2189. }
  2190. avctx->coded_frame= &s->current_picture;
  2191. switch(avctx->pix_fmt){
  2192. // case PIX_FMT_YUV444P:
  2193. // case PIX_FMT_YUV422P:
  2194. case PIX_FMT_YUV420P:
  2195. case PIX_FMT_GRAY8:
  2196. // case PIX_FMT_YUV411P:
  2197. // case PIX_FMT_YUV410P:
  2198. s->colorspace_type= 0;
  2199. break;
  2200. /* case PIX_FMT_RGBA32:
  2201. s->colorspace= 1;
  2202. break;*/
  2203. default:
  2204. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  2205. return -1;
  2206. }
  2207. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  2208. s->chroma_h_shift= 1;
  2209. s->chroma_v_shift= 1;
  2210. return 0;
  2211. }
  2212. static int frame_start(SnowContext *s){
  2213. AVFrame tmp;
  2214. int w= s->avctx->width; //FIXME round up to x16 ?
  2215. int h= s->avctx->height;
  2216. if(s->keyframe)
  2217. reset_contexts(s);
  2218. if(s->current_picture.data[0]){
  2219. draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  2220. draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  2221. draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  2222. }
  2223. tmp= s->last_picture;
  2224. s->last_picture= s->current_picture;
  2225. s->current_picture= tmp;
  2226. s->current_picture.reference= 1;
  2227. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  2228. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  2229. return -1;
  2230. }
  2231. return 0;
  2232. }
  2233. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  2234. SnowContext *s = avctx->priv_data;
  2235. CABACContext * const c= &s->c;
  2236. AVFrame *pict = data;
  2237. const int width= s->avctx->width;
  2238. const int height= s->avctx->height;
  2239. int level, orientation, plane_index;
  2240. ff_init_cabac_encoder(c, buf, buf_size);
  2241. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2242. s->input_picture = *pict;
  2243. memset(s->header_state, 0, sizeof(s->header_state));
  2244. s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  2245. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  2246. if(pict->quality){
  2247. s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
  2248. //<64 >60
  2249. s->qlog += 61;
  2250. }else{
  2251. s->qlog= LOSSLESS_QLOG;
  2252. }
  2253. frame_start(s);
  2254. if(pict->pict_type == P_TYPE){
  2255. int block_width = (width +15)>>4;
  2256. int block_height= (height+15)>>4;
  2257. int stride= s->current_picture.linesize[0];
  2258. assert(s->current_picture.data[0]);
  2259. assert(s->last_picture.data[0]);
  2260. s->m.avctx= s->avctx;
  2261. s->m.current_picture.data[0]= s->current_picture.data[0];
  2262. s->m. last_picture.data[0]= s-> last_picture.data[0];
  2263. s->m. new_picture.data[0]= s-> input_picture.data[0];
  2264. s->m.current_picture_ptr= &s->m.current_picture;
  2265. s->m. last_picture_ptr= &s->m. last_picture;
  2266. s->m.linesize=
  2267. s->m. last_picture.linesize[0]=
  2268. s->m. new_picture.linesize[0]=
  2269. s->m.current_picture.linesize[0]= stride;
  2270. s->m.uvlinesize= s->current_picture.linesize[1];
  2271. s->m.width = width;
  2272. s->m.height= height;
  2273. s->m.mb_width = block_width;
  2274. s->m.mb_height= block_height;
  2275. s->m.mb_stride= s->m.mb_width+1;
  2276. s->m.b8_stride= 2*s->m.mb_width+1;
  2277. s->m.f_code=1;
  2278. s->m.pict_type= pict->pict_type;
  2279. s->m.me_method= s->avctx->me_method;
  2280. s->m.me.scene_change_score=0;
  2281. s->m.flags= s->avctx->flags;
  2282. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  2283. s->m.out_format= FMT_H263;
  2284. s->m.unrestricted_mv= 1;
  2285. s->lambda = s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else
  2286. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  2287. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  2288. s->m.dsp= s->dsp; //move
  2289. ff_init_me(&s->m);
  2290. }
  2291. redo_frame:
  2292. s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
  2293. encode_header(s);
  2294. encode_blocks(s);
  2295. for(plane_index=0; plane_index<3; plane_index++){
  2296. Plane *p= &s->plane[plane_index];
  2297. int w= p->width;
  2298. int h= p->height;
  2299. int x, y;
  2300. // int bits= put_bits_count(&s->c.pb);
  2301. //FIXME optimize
  2302. if(pict->data[plane_index]) //FIXME gray hack
  2303. for(y=0; y<h; y++){
  2304. for(x=0; x<w; x++){
  2305. s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;
  2306. }
  2307. }
  2308. predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
  2309. if( plane_index==0
  2310. && pict->pict_type == P_TYPE
  2311. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  2312. ff_init_cabac_encoder(c, buf, buf_size);
  2313. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2314. pict->pict_type= FF_I_TYPE;
  2315. s->keyframe=1;
  2316. reset_contexts(s);
  2317. goto redo_frame;
  2318. }
  2319. if(s->qlog == LOSSLESS_QLOG){
  2320. for(y=0; y<h; y++){
  2321. for(x=0; x<w; x++){
  2322. s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + 127)>>8;
  2323. }
  2324. }
  2325. }
  2326. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2327. for(level=0; level<s->spatial_decomposition_count; level++){
  2328. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2329. SubBand *b= &p->band[level][orientation];
  2330. quantize(s, b, b->buf, b->stride, s->qbias);
  2331. if(orientation==0)
  2332. decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
  2333. encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  2334. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  2335. if(orientation==0)
  2336. correlate(s, b, b->buf, b->stride, 1, 0);
  2337. }
  2338. }
  2339. // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
  2340. for(level=0; level<s->spatial_decomposition_count; level++){
  2341. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2342. SubBand *b= &p->band[level][orientation];
  2343. dequantize(s, b, b->buf, b->stride);
  2344. }
  2345. }
  2346. ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2347. if(s->qlog == LOSSLESS_QLOG){
  2348. for(y=0; y<h; y++){
  2349. for(x=0; x<w; x++){
  2350. s->spatial_dwt_buffer[y*w + x]<<=8;
  2351. }
  2352. }
  2353. }
  2354. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2355. //FIXME optimize
  2356. for(y=0; y<h; y++){
  2357. for(x=0; x<w; x++){
  2358. int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
  2359. if(v&(~255)) v= ~(v>>31);
  2360. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;
  2361. }
  2362. }
  2363. if(s->avctx->flags&CODEC_FLAG_PSNR){
  2364. int64_t error= 0;
  2365. if(pict->data[plane_index]) //FIXME gray hack
  2366. for(y=0; y<h; y++){
  2367. for(x=0; x<w; x++){
  2368. int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
  2369. error += d*d;
  2370. }
  2371. }
  2372. s->avctx->error[plane_index] += error;
  2373. s->avctx->error[3] += error;
  2374. }
  2375. }
  2376. if(s->last_picture.data[0])
  2377. avctx->release_buffer(avctx, &s->last_picture);
  2378. emms_c();
  2379. return put_cabac_terminate(c, 1);
  2380. }
  2381. static void common_end(SnowContext *s){
  2382. int plane_index, level, orientation;
  2383. av_freep(&s->spatial_dwt_buffer);
  2384. av_freep(&s->m.me.scratchpad);
  2385. av_freep(&s->m.me.map);
  2386. av_freep(&s->m.me.score_map);
  2387. av_freep(&s->block);
  2388. for(plane_index=0; plane_index<3; plane_index++){
  2389. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  2390. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2391. SubBand *b= &s->plane[plane_index].band[level][orientation];
  2392. av_freep(&b->x);
  2393. av_freep(&b->coeff);
  2394. }
  2395. }
  2396. }
  2397. }
  2398. static int encode_end(AVCodecContext *avctx)
  2399. {
  2400. SnowContext *s = avctx->priv_data;
  2401. common_end(s);
  2402. return 0;
  2403. }
  2404. static int decode_init(AVCodecContext *avctx)
  2405. {
  2406. // SnowContext *s = avctx->priv_data;
  2407. common_init(avctx);
  2408. return 0;
  2409. }
  2410. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  2411. SnowContext *s = avctx->priv_data;
  2412. CABACContext * const c= &s->c;
  2413. int bytes_read;
  2414. AVFrame *picture = data;
  2415. int level, orientation, plane_index;
  2416. /* no supplementary picture */
  2417. if (buf_size == 0)
  2418. return 0;
  2419. ff_init_cabac_decoder(c, buf, buf_size);
  2420. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2421. memset(s->header_state, 0, sizeof(s->header_state));
  2422. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  2423. decode_header(s);
  2424. if(!s->block) alloc_blocks(s);
  2425. frame_start(s);
  2426. //keyframe flag dupliaction mess FIXME
  2427. if(avctx->debug&FF_DEBUG_PICT_INFO)
  2428. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  2429. decode_blocks(s);
  2430. for(plane_index=0; plane_index<3; plane_index++){
  2431. Plane *p= &s->plane[plane_index];
  2432. int w= p->width;
  2433. int h= p->height;
  2434. int x, y;
  2435. if(s->avctx->debug&2048){
  2436. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  2437. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2438. for(y=0; y<h; y++){
  2439. for(x=0; x<w; x++){
  2440. int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
  2441. if(v&(~255)) v= ~(v>>31);
  2442. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  2443. }
  2444. }
  2445. }
  2446. for(level=0; level<s->spatial_decomposition_count; level++){
  2447. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2448. SubBand *b= &p->band[level][orientation];
  2449. decode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  2450. if(orientation==0){
  2451. correlate(s, b, b->buf, b->stride, 1, 0);
  2452. dequantize(s, b, b->buf, b->stride);
  2453. assert(b->buf == s->spatial_dwt_buffer);
  2454. }
  2455. }
  2456. }
  2457. ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2458. if(s->qlog == LOSSLESS_QLOG){
  2459. for(y=0; y<h; y++){
  2460. for(x=0; x<w; x++){
  2461. s->spatial_dwt_buffer[y*w + x]<<=8;
  2462. }
  2463. }
  2464. }
  2465. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2466. //FIXME optimize
  2467. for(y=0; y<h; y++){
  2468. for(x=0; x<w; x++){
  2469. int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
  2470. if(v&(~255)) v= ~(v>>31);
  2471. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;
  2472. }
  2473. }
  2474. }
  2475. emms_c();
  2476. if(s->last_picture.data[0])
  2477. avctx->release_buffer(avctx, &s->last_picture);
  2478. if(!(s->avctx->debug&2048))
  2479. *picture= s->current_picture;
  2480. else
  2481. *picture= s->mconly_picture;
  2482. *data_size = sizeof(AVFrame);
  2483. bytes_read= get_cabac_terminate(c);
  2484. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n");
  2485. return bytes_read;
  2486. }
  2487. static int decode_end(AVCodecContext *avctx)
  2488. {
  2489. SnowContext *s = avctx->priv_data;
  2490. common_end(s);
  2491. return 0;
  2492. }
  2493. AVCodec snow_decoder = {
  2494. "snow",
  2495. CODEC_TYPE_VIDEO,
  2496. CODEC_ID_SNOW,
  2497. sizeof(SnowContext),
  2498. decode_init,
  2499. NULL,
  2500. decode_end,
  2501. decode_frame,
  2502. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  2503. NULL
  2504. };
  2505. AVCodec snow_encoder = {
  2506. "snow",
  2507. CODEC_TYPE_VIDEO,
  2508. CODEC_ID_SNOW,
  2509. sizeof(SnowContext),
  2510. encode_init,
  2511. encode_frame,
  2512. encode_end,
  2513. };
  2514. #if 0
  2515. #undef malloc
  2516. #undef free
  2517. #undef printf
  2518. int main(){
  2519. int width=256;
  2520. int height=256;
  2521. int buffer[2][width*height];
  2522. SnowContext s;
  2523. int i;
  2524. s.spatial_decomposition_count=6;
  2525. s.spatial_decomposition_type=1;
  2526. printf("testing 5/3 DWT\n");
  2527. for(i=0; i<width*height; i++)
  2528. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  2529. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2530. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2531. for(i=0; i<width*height; i++)
  2532. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  2533. printf("testing 9/7 DWT\n");
  2534. s.spatial_decomposition_type=0;
  2535. for(i=0; i<width*height; i++)
  2536. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  2537. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2538. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2539. for(i=0; i<width*height; i++)
  2540. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  2541. printf("testing AC coder\n");
  2542. memset(s.header_state, 0, sizeof(s.header_state));
  2543. ff_init_cabac_encoder(&s.c, buffer[0], 256*256);
  2544. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2545. for(i=-256; i<256; i++){
  2546. START_TIMER
  2547. put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
  2548. STOP_TIMER("put_symbol")
  2549. }
  2550. put_cabac_terminate(&s.c, 1);
  2551. memset(s.header_state, 0, sizeof(s.header_state));
  2552. ff_init_cabac_decoder(&s.c, buffer[0], 256*256);
  2553. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2554. for(i=-256; i<256; i++){
  2555. int j;
  2556. START_TIMER
  2557. j= get_symbol(&s.c, s.header_state, 1);
  2558. STOP_TIMER("get_symbol")
  2559. if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
  2560. }
  2561. {
  2562. int level, orientation, x, y;
  2563. int64_t errors[8][4];
  2564. int64_t g=0;
  2565. memset(errors, 0, sizeof(errors));
  2566. s.spatial_decomposition_count=3;
  2567. s.spatial_decomposition_type=0;
  2568. for(level=0; level<s.spatial_decomposition_count; level++){
  2569. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2570. int w= width >> (s.spatial_decomposition_count-level);
  2571. int h= height >> (s.spatial_decomposition_count-level);
  2572. int stride= width << (s.spatial_decomposition_count-level);
  2573. DWTELEM *buf= buffer[0];
  2574. int64_t error=0;
  2575. if(orientation&1) buf+=w;
  2576. if(orientation>1) buf+=stride>>1;
  2577. memset(buffer[0], 0, sizeof(int)*width*height);
  2578. buf[w/2 + h/2*stride]= 256*256;
  2579. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2580. for(y=0; y<height; y++){
  2581. for(x=0; x<width; x++){
  2582. int64_t d= buffer[0][x + y*width];
  2583. error += d*d;
  2584. if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
  2585. }
  2586. if(ABS(height/2-y)<9 && level==2) printf("\n");
  2587. }
  2588. error= (int)(sqrt(error)+0.5);
  2589. errors[level][orientation]= error;
  2590. if(g) g=ff_gcd(g, error);
  2591. else g= error;
  2592. }
  2593. }
  2594. printf("static int const visual_weight[][4]={\n");
  2595. for(level=0; level<s.spatial_decomposition_count; level++){
  2596. printf(" {");
  2597. for(orientation=0; orientation<4; orientation++){
  2598. printf("%8lld,", errors[level][orientation]/g);
  2599. }
  2600. printf("},\n");
  2601. }
  2602. printf("};\n");
  2603. {
  2604. int level=2;
  2605. int orientation=3;
  2606. int w= width >> (s.spatial_decomposition_count-level);
  2607. int h= height >> (s.spatial_decomposition_count-level);
  2608. int stride= width << (s.spatial_decomposition_count-level);
  2609. DWTELEM *buf= buffer[0];
  2610. int64_t error=0;
  2611. buf+=w;
  2612. buf+=stride>>1;
  2613. memset(buffer[0], 0, sizeof(int)*width*height);
  2614. #if 1
  2615. for(y=0; y<height; y++){
  2616. for(x=0; x<width; x++){
  2617. int tab[4]={0,2,3,1};
  2618. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  2619. }
  2620. }
  2621. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2622. #else
  2623. for(y=0; y<h; y++){
  2624. for(x=0; x<w; x++){
  2625. buf[x + y*stride ]=169;
  2626. buf[x + y*stride-w]=64;
  2627. }
  2628. }
  2629. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2630. #endif
  2631. for(y=0; y<height; y++){
  2632. for(x=0; x<width; x++){
  2633. int64_t d= buffer[0][x + y*width];
  2634. error += d*d;
  2635. if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
  2636. }
  2637. if(ABS(height/2-y)<9) printf("\n");
  2638. }
  2639. }
  2640. }
  2641. return 0;
  2642. }
  2643. #endif