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.

2873 lines
99KB

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