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.

3568 lines
123KB

  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 inline void put_symbol2(CABACContext *c, uint8_t *state, int v, int log2){
  469. int i;
  470. int r= log2>=0 ? 1<<log2 : 1;
  471. assert(v>=0);
  472. assert(log2>=-4);
  473. while(v >= r){
  474. put_cabac(c, state+4+log2, 1);
  475. v -= r;
  476. log2++;
  477. if(log2>0) r+=r;
  478. }
  479. put_cabac(c, state+4+log2, 0);
  480. for(i=log2-1; i>=0; i--){
  481. put_cabac(c, state+31-i, (v>>i)&1);
  482. }
  483. }
  484. static inline int get_symbol2(CABACContext *c, uint8_t *state, int log2){
  485. int i;
  486. int r= log2>=0 ? 1<<log2 : 1;
  487. int v=0;
  488. assert(log2>=-4);
  489. while(get_cabac(c, state+4+log2)){
  490. v+= r;
  491. log2++;
  492. if(log2>0) r+=r;
  493. }
  494. for(i=log2-1; i>=0; i--){
  495. v+= get_cabac(c, state+31-i)<<i;
  496. }
  497. return v;
  498. }
  499. 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){
  500. const int mirror_left= !highpass;
  501. const int mirror_right= (width&1) ^ highpass;
  502. const int w= (width>>1) - 1 + (highpass & width);
  503. int i;
  504. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  505. if(mirror_left){
  506. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  507. dst += dst_step;
  508. src += src_step;
  509. }
  510. for(i=0; i<w; i++){
  511. dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
  512. }
  513. if(mirror_right){
  514. dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
  515. }
  516. }
  517. 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){
  518. const int mirror_left= !highpass;
  519. const int mirror_right= (width&1) ^ highpass;
  520. const int w= (width>>1) - 1 + (highpass & width);
  521. int i;
  522. if(mirror_left){
  523. int r= 3*2*ref[0];
  524. r += r>>4;
  525. r += r>>8;
  526. dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
  527. dst += dst_step;
  528. src += src_step;
  529. }
  530. for(i=0; i<w; i++){
  531. int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
  532. r += r>>4;
  533. r += r>>8;
  534. dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
  535. }
  536. if(mirror_right){
  537. int r= 3*2*ref[w*ref_step];
  538. r += r>>4;
  539. r += r>>8;
  540. dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
  541. }
  542. }
  543. static void inplace_lift(int *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
  544. int x, i;
  545. for(x=start; x<width; x+=2){
  546. int64_t sum=0;
  547. for(i=0; i<n; i++){
  548. int x2= x + 2*i - n + 1;
  549. if (x2< 0) x2= -x2;
  550. else if(x2>=width) x2= 2*width-x2-2;
  551. sum += coeffs[i]*(int64_t)dst[x2];
  552. }
  553. if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
  554. else dst[x] += (sum + (1<<shift)/2)>>shift;
  555. }
  556. }
  557. static void inplace_liftV(int *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
  558. int x, y, i;
  559. for(y=start; y<height; y+=2){
  560. for(x=0; x<width; x++){
  561. int64_t sum=0;
  562. for(i=0; i<n; i++){
  563. int y2= y + 2*i - n + 1;
  564. if (y2< 0) y2= -y2;
  565. else if(y2>=height) y2= 2*height-y2-2;
  566. sum += coeffs[i]*(int64_t)dst[x + y2*stride];
  567. }
  568. if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
  569. else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
  570. }
  571. }
  572. }
  573. #define SCALEX 1
  574. #define LX0 0
  575. #define LX1 1
  576. #if 0 // more accurate 9/7
  577. #define N1 2
  578. #define SHIFT1 14
  579. #define COEFFS1 (int[]){-25987,-25987}
  580. #define N2 2
  581. #define SHIFT2 19
  582. #define COEFFS2 (int[]){-27777,-27777}
  583. #define N3 2
  584. #define SHIFT3 15
  585. #define COEFFS3 (int[]){28931,28931}
  586. #define N4 2
  587. #define SHIFT4 15
  588. #define COEFFS4 (int[]){14533,14533}
  589. #elif 1 // 13/7 CRF
  590. #define N1 4
  591. #define SHIFT1 4
  592. #define COEFFS1 (int[]){1,-9,-9,1}
  593. #define N2 4
  594. #define SHIFT2 4
  595. #define COEFFS2 (int[]){-1,5,5,-1}
  596. #define N3 0
  597. #define SHIFT3 1
  598. #define COEFFS3 NULL
  599. #define N4 0
  600. #define SHIFT4 1
  601. #define COEFFS4 NULL
  602. #elif 1 // 3/5
  603. #define LX0 1
  604. #define LX1 0
  605. #define SCALEX 0.5
  606. #define N1 2
  607. #define SHIFT1 1
  608. #define COEFFS1 (int[]){1,1}
  609. #define N2 2
  610. #define SHIFT2 2
  611. #define COEFFS2 (int[]){-1,-1}
  612. #define N3 0
  613. #define SHIFT3 0
  614. #define COEFFS3 NULL
  615. #define N4 0
  616. #define SHIFT4 0
  617. #define COEFFS4 NULL
  618. #elif 1 // 11/5
  619. #define N1 0
  620. #define SHIFT1 1
  621. #define COEFFS1 NULL
  622. #define N2 2
  623. #define SHIFT2 2
  624. #define COEFFS2 (int[]){-1,-1}
  625. #define N3 2
  626. #define SHIFT3 0
  627. #define COEFFS3 (int[]){-1,-1}
  628. #define N4 4
  629. #define SHIFT4 7
  630. #define COEFFS4 (int[]){-5,29,29,-5}
  631. #define SCALEX 4
  632. #elif 1 // 9/7 CDF
  633. #define N1 2
  634. #define SHIFT1 7
  635. #define COEFFS1 (int[]){-203,-203}
  636. #define N2 2
  637. #define SHIFT2 12
  638. #define COEFFS2 (int[]){-217,-217}
  639. #define N3 2
  640. #define SHIFT3 7
  641. #define COEFFS3 (int[]){113,113}
  642. #define N4 2
  643. #define SHIFT4 9
  644. #define COEFFS4 (int[]){227,227}
  645. #define SCALEX 1
  646. #elif 1 // 7/5 CDF
  647. #define N1 0
  648. #define SHIFT1 1
  649. #define COEFFS1 NULL
  650. #define N2 2
  651. #define SHIFT2 2
  652. #define COEFFS2 (int[]){-1,-1}
  653. #define N3 2
  654. #define SHIFT3 0
  655. #define COEFFS3 (int[]){-1,-1}
  656. #define N4 2
  657. #define SHIFT4 4
  658. #define COEFFS4 (int[]){3,3}
  659. #elif 1 // 9/7 MN
  660. #define N1 4
  661. #define SHIFT1 4
  662. #define COEFFS1 (int[]){1,-9,-9,1}
  663. #define N2 2
  664. #define SHIFT2 2
  665. #define COEFFS2 (int[]){1,1}
  666. #define N3 0
  667. #define SHIFT3 1
  668. #define COEFFS3 NULL
  669. #define N4 0
  670. #define SHIFT4 1
  671. #define COEFFS4 NULL
  672. #else // 13/7 CRF
  673. #define N1 4
  674. #define SHIFT1 4
  675. #define COEFFS1 (int[]){1,-9,-9,1}
  676. #define N2 4
  677. #define SHIFT2 4
  678. #define COEFFS2 (int[]){-1,5,5,-1}
  679. #define N3 0
  680. #define SHIFT3 1
  681. #define COEFFS3 NULL
  682. #define N4 0
  683. #define SHIFT4 1
  684. #define COEFFS4 NULL
  685. #endif
  686. static void horizontal_decomposeX(int *b, int width){
  687. int temp[width];
  688. const int width2= width>>1;
  689. const int w2= (width+1)>>1;
  690. int A1,A2,A3,A4, x;
  691. inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
  692. inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
  693. inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
  694. inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
  695. for(x=0; x<width2; x++){
  696. temp[x ]= b[2*x ];
  697. temp[x+w2]= b[2*x + 1];
  698. }
  699. if(width&1)
  700. temp[x ]= b[2*x ];
  701. memcpy(b, temp, width*sizeof(int));
  702. }
  703. static void horizontal_composeX(int *b, int width){
  704. int temp[width];
  705. const int width2= width>>1;
  706. int A1,A2,A3,A4, x;
  707. const int w2= (width+1)>>1;
  708. memcpy(temp, b, width*sizeof(int));
  709. for(x=0; x<width2; x++){
  710. b[2*x ]= temp[x ];
  711. b[2*x + 1]= temp[x+w2];
  712. }
  713. if(width&1)
  714. b[2*x ]= temp[x ];
  715. inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
  716. inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
  717. inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
  718. inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
  719. }
  720. static void spatial_decomposeX(int *buffer, int width, int height, int stride){
  721. int x, y;
  722. for(y=0; y<height; y++){
  723. for(x=0; x<width; x++){
  724. buffer[y*stride + x] *= SCALEX;
  725. }
  726. }
  727. for(y=0; y<height; y++){
  728. horizontal_decomposeX(buffer + y*stride, width);
  729. }
  730. inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
  731. inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
  732. inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
  733. inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
  734. }
  735. static void spatial_composeX(int *buffer, int width, int height, int stride){
  736. int x, y;
  737. inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
  738. inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
  739. inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
  740. inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
  741. for(y=0; y<height; y++){
  742. horizontal_composeX(buffer + y*stride, width);
  743. }
  744. for(y=0; y<height; y++){
  745. for(x=0; x<width; x++){
  746. buffer[y*stride + x] /= SCALEX;
  747. }
  748. }
  749. }
  750. static void horizontal_decompose53i(int *b, int width){
  751. int temp[width];
  752. const int width2= width>>1;
  753. int A1,A2,A3,A4, x;
  754. const int w2= (width+1)>>1;
  755. for(x=0; x<width2; x++){
  756. temp[x ]= b[2*x ];
  757. temp[x+w2]= b[2*x + 1];
  758. }
  759. if(width&1)
  760. temp[x ]= b[2*x ];
  761. #if 0
  762. A2= temp[1 ];
  763. A4= temp[0 ];
  764. A1= temp[0+width2];
  765. A1 -= (A2 + A4)>>1;
  766. A4 += (A1 + 1)>>1;
  767. b[0+width2] = A1;
  768. b[0 ] = A4;
  769. for(x=1; x+1<width2; x+=2){
  770. A3= temp[x+width2];
  771. A4= temp[x+1 ];
  772. A3 -= (A2 + A4)>>1;
  773. A2 += (A1 + A3 + 2)>>2;
  774. b[x+width2] = A3;
  775. b[x ] = A2;
  776. A1= temp[x+1+width2];
  777. A2= temp[x+2 ];
  778. A1 -= (A2 + A4)>>1;
  779. A4 += (A1 + A3 + 2)>>2;
  780. b[x+1+width2] = A1;
  781. b[x+1 ] = A4;
  782. }
  783. A3= temp[width-1];
  784. A3 -= A2;
  785. A2 += (A1 + A3 + 2)>>2;
  786. b[width -1] = A3;
  787. b[width2-1] = A2;
  788. #else
  789. lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  790. lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  791. #endif
  792. }
  793. static void vertical_decompose53iH0(int *b0, int *b1, int *b2, int width){
  794. int i;
  795. for(i=0; i<width; i++){
  796. b1[i] -= (b0[i] + b2[i])>>1;
  797. }
  798. }
  799. static void vertical_decompose53iL0(int *b0, int *b1, int *b2, int width){
  800. int i;
  801. for(i=0; i<width; i++){
  802. b1[i] += (b0[i] + b2[i] + 2)>>2;
  803. }
  804. }
  805. static void spatial_decompose53i(int *buffer, int width, int height, int stride){
  806. int x, y;
  807. DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
  808. DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
  809. for(y=-2; y<height; y+=2){
  810. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  811. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  812. {START_TIMER
  813. if(b1 <= b3) horizontal_decompose53i(b2, width);
  814. if(y+2 < height) horizontal_decompose53i(b3, width);
  815. STOP_TIMER("horizontal_decompose53i")}
  816. {START_TIMER
  817. if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
  818. if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
  819. STOP_TIMER("vertical_decompose53i*")}
  820. b0=b2;
  821. b1=b3;
  822. }
  823. }
  824. #define lift5 lift
  825. #if 1
  826. #define W_AM 3
  827. #define W_AO 0
  828. #define W_AS 1
  829. #define W_BM 1
  830. #define W_BO 8
  831. #define W_BS 4
  832. #undef lift5
  833. #define W_CM 9999
  834. #define W_CO 2
  835. #define W_CS 2
  836. #define W_DM 15
  837. #define W_DO 16
  838. #define W_DS 5
  839. #elif 0
  840. #define W_AM 55
  841. #define W_AO 16
  842. #define W_AS 5
  843. #define W_BM 3
  844. #define W_BO 32
  845. #define W_BS 6
  846. #define W_CM 127
  847. #define W_CO 64
  848. #define W_CS 7
  849. #define W_DM 7
  850. #define W_DO 8
  851. #define W_DS 4
  852. #elif 0
  853. #define W_AM 97
  854. #define W_AO 32
  855. #define W_AS 6
  856. #define W_BM 63
  857. #define W_BO 512
  858. #define W_BS 10
  859. #define W_CM 13
  860. #define W_CO 8
  861. #define W_CS 4
  862. #define W_DM 15
  863. #define W_DO 16
  864. #define W_DS 5
  865. #else
  866. #define W_AM 203
  867. #define W_AO 64
  868. #define W_AS 7
  869. #define W_BM 217
  870. #define W_BO 2048
  871. #define W_BS 12
  872. #define W_CM 113
  873. #define W_CO 64
  874. #define W_CS 7
  875. #define W_DM 227
  876. #define W_DO 128
  877. #define W_DS 9
  878. #endif
  879. static void horizontal_decompose97i(int *b, int width){
  880. int temp[width];
  881. const int w2= (width+1)>>1;
  882. lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
  883. lift (temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
  884. lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  885. lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  886. }
  887. static void vertical_decompose97iH0(int *b0, int *b1, int *b2, int width){
  888. int i;
  889. for(i=0; i<width; i++){
  890. b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  891. }
  892. }
  893. static void vertical_decompose97iH1(int *b0, int *b1, int *b2, int width){
  894. int i;
  895. for(i=0; i<width; i++){
  896. #ifdef lift5
  897. b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  898. #else
  899. int r= 3*(b0[i] + b2[i]);
  900. r+= r>>4;
  901. r+= r>>8;
  902. b1[i] += (r+W_CO)>>W_CS;
  903. #endif
  904. }
  905. }
  906. static void vertical_decompose97iL0(int *b0, int *b1, int *b2, int width){
  907. int i;
  908. for(i=0; i<width; i++){
  909. b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  910. }
  911. }
  912. static void vertical_decompose97iL1(int *b0, int *b1, int *b2, int width){
  913. int i;
  914. for(i=0; i<width; i++){
  915. b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  916. }
  917. }
  918. static void spatial_decompose97i(int *buffer, int width, int height, int stride){
  919. int x, y;
  920. DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
  921. DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
  922. DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
  923. DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
  924. for(y=-4; y<height; y+=2){
  925. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  926. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  927. {START_TIMER
  928. if(b3 <= b5) horizontal_decompose97i(b4, width);
  929. if(y+4 < height) horizontal_decompose97i(b5, width);
  930. if(width>400){
  931. STOP_TIMER("horizontal_decompose97i")
  932. }}
  933. {START_TIMER
  934. if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
  935. if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
  936. if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
  937. if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
  938. if(width>400){
  939. STOP_TIMER("vertical_decompose97i")
  940. }}
  941. b0=b2;
  942. b1=b3;
  943. b2=b4;
  944. b3=b5;
  945. }
  946. }
  947. static void spatial_dwt(SnowContext *s, int *buffer, int width, int height, int stride){
  948. int level;
  949. for(level=0; level<s->spatial_decomposition_count; level++){
  950. switch(s->spatial_decomposition_type){
  951. case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
  952. case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
  953. case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
  954. }
  955. }
  956. }
  957. static void horizontal_compose53i(int *b, int width){
  958. int temp[width];
  959. const int width2= width>>1;
  960. const int w2= (width+1)>>1;
  961. int A1,A2,A3,A4, x;
  962. #if 0
  963. A2= temp[1 ];
  964. A4= temp[0 ];
  965. A1= temp[0+width2];
  966. A1 -= (A2 + A4)>>1;
  967. A4 += (A1 + 1)>>1;
  968. b[0+width2] = A1;
  969. b[0 ] = A4;
  970. for(x=1; x+1<width2; x+=2){
  971. A3= temp[x+width2];
  972. A4= temp[x+1 ];
  973. A3 -= (A2 + A4)>>1;
  974. A2 += (A1 + A3 + 2)>>2;
  975. b[x+width2] = A3;
  976. b[x ] = A2;
  977. A1= temp[x+1+width2];
  978. A2= temp[x+2 ];
  979. A1 -= (A2 + A4)>>1;
  980. A4 += (A1 + A3 + 2)>>2;
  981. b[x+1+width2] = A1;
  982. b[x+1 ] = A4;
  983. }
  984. A3= temp[width-1];
  985. A3 -= A2;
  986. A2 += (A1 + A3 + 2)>>2;
  987. b[width -1] = A3;
  988. b[width2-1] = A2;
  989. #else
  990. lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
  991. lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
  992. #endif
  993. for(x=0; x<width2; x++){
  994. b[2*x ]= temp[x ];
  995. b[2*x + 1]= temp[x+w2];
  996. }
  997. if(width&1)
  998. b[2*x ]= temp[x ];
  999. }
  1000. static void vertical_compose53iH0(int *b0, int *b1, int *b2, int width){
  1001. int i;
  1002. for(i=0; i<width; i++){
  1003. b1[i] += (b0[i] + b2[i])>>1;
  1004. }
  1005. }
  1006. static void vertical_compose53iL0(int *b0, int *b1, int *b2, int width){
  1007. int i;
  1008. for(i=0; i<width; i++){
  1009. b1[i] -= (b0[i] + b2[i] + 2)>>2;
  1010. }
  1011. }
  1012. static void spatial_compose53i(int *buffer, int width, int height, int stride){
  1013. int x, y;
  1014. DWTELEM *b0= buffer + mirror(-1-1, height-1)*stride;
  1015. DWTELEM *b1= buffer + mirror(-1 , height-1)*stride;
  1016. for(y=-1; y<=height; y+=2){
  1017. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  1018. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  1019. {START_TIMER
  1020. if(b1 <= b3) vertical_compose53iL0(b1, b2, b3, width);
  1021. if(b0 <= b2) vertical_compose53iH0(b0, b1, b2, width);
  1022. STOP_TIMER("vertical_compose53i*")}
  1023. {START_TIMER
  1024. if(y-1 >= 0) horizontal_compose53i(b0, width);
  1025. if(b0 <= b2) horizontal_compose53i(b1, width);
  1026. STOP_TIMER("horizontal_compose53i")}
  1027. b0=b2;
  1028. b1=b3;
  1029. }
  1030. }
  1031. static void horizontal_compose97i(int *b, int width){
  1032. int temp[width];
  1033. const int w2= (width+1)>>1;
  1034. lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  1035. lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
  1036. lift (b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
  1037. lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
  1038. }
  1039. static void vertical_compose97iH0(int *b0, int *b1, int *b2, int width){
  1040. int i;
  1041. for(i=0; i<width; i++){
  1042. b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  1043. }
  1044. }
  1045. static void vertical_compose97iH1(int *b0, int *b1, int *b2, int width){
  1046. int i;
  1047. for(i=0; i<width; i++){
  1048. #ifdef lift5
  1049. b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  1050. #else
  1051. int r= 3*(b0[i] + b2[i]);
  1052. r+= r>>4;
  1053. r+= r>>8;
  1054. b1[i] -= (r+W_CO)>>W_CS;
  1055. #endif
  1056. }
  1057. }
  1058. static void vertical_compose97iL0(int *b0, int *b1, int *b2, int width){
  1059. int i;
  1060. for(i=0; i<width; i++){
  1061. b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  1062. }
  1063. }
  1064. static void vertical_compose97iL1(int *b0, int *b1, int *b2, int width){
  1065. int i;
  1066. for(i=0; i<width; i++){
  1067. b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  1068. }
  1069. }
  1070. static void spatial_compose97i(int *buffer, int width, int height, int stride){
  1071. int x, y;
  1072. DWTELEM *b0= buffer + mirror(-3-1, height-1)*stride;
  1073. DWTELEM *b1= buffer + mirror(-3 , height-1)*stride;
  1074. DWTELEM *b2= buffer + mirror(-3+1, height-1)*stride;
  1075. DWTELEM *b3= buffer + mirror(-3+2, height-1)*stride;
  1076. for(y=-3; y<=height; y+=2){
  1077. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  1078. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  1079. if(stride == width && y+4 < height && 0){
  1080. int x;
  1081. for(x=0; x<width/2; x++)
  1082. b5[x] += 64*2;
  1083. for(; x<width; x++)
  1084. b5[x] += 169*2;
  1085. }
  1086. {START_TIMER
  1087. if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
  1088. if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
  1089. if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
  1090. if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
  1091. if(width>400){
  1092. STOP_TIMER("vertical_compose97i")}}
  1093. {START_TIMER
  1094. if(y-1>= 0) horizontal_compose97i(b0, width);
  1095. if(b0 <= b2) horizontal_compose97i(b1, width);
  1096. if(width>400 && b0 <= b2){
  1097. STOP_TIMER("horizontal_compose97i")}}
  1098. b0=b2;
  1099. b1=b3;
  1100. b2=b4;
  1101. b3=b5;
  1102. }
  1103. }
  1104. static void spatial_idwt(SnowContext *s, int *buffer, int width, int height, int stride){
  1105. int level;
  1106. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  1107. switch(s->spatial_decomposition_type){
  1108. case 0: spatial_compose97i(buffer, width>>level, height>>level, stride<<level); break;
  1109. case 1: spatial_compose53i(buffer, width>>level, height>>level, stride<<level); break;
  1110. case 2: spatial_composeX (buffer, width>>level, height>>level, stride<<level); break;
  1111. }
  1112. }
  1113. }
  1114. static const int hilbert[16][2]={
  1115. {0,0}, {1,0}, {1,1}, {0,1},
  1116. {0,2}, {0,3}, {1,3}, {1,2},
  1117. {2,2}, {2,3}, {3,3}, {3,2},
  1118. {3,1}, {2,1}, {2,0}, {3,0},
  1119. };
  1120. #if 0
  1121. -o o-
  1122. | |
  1123. o-o
  1124. -o-o o-o-
  1125. | |
  1126. o-o o-o
  1127. | |
  1128. o o-o o
  1129. | | | |
  1130. o-o o-o
  1131. 0112122312232334122323342334
  1132. 0123456789ABCDEF0123456789AB
  1133. RLLRMRRLLRRMRLLMLRRLMLLRRLLM
  1134. 4 B F 14 1B
  1135. 4 11 15 20 27
  1136. -o o-o-o o-o-o o-
  1137. | | | | | |
  1138. o-o o-o o-o o-o
  1139. | |
  1140. o-o o-o o-o o-o
  1141. | | | | | |
  1142. o o-o-o o-o-o o
  1143. | |
  1144. o-o o-o-o-o o-o
  1145. | | | |
  1146. o-o o-o o-o o-o
  1147. | | | |
  1148. o o-o o o o-o o
  1149. | | | | | | | |
  1150. o-o o-o o-o o-o
  1151. #endif
  1152. #define SVI(a, i, x, y) \
  1153. {\
  1154. a[i][0]= x;\
  1155. a[i][1]= y;\
  1156. i++;\
  1157. }
  1158. static int sig_cmp(const void *a, const void *b){
  1159. const int16_t* da = (const int16_t *) a;
  1160. const int16_t* db = (const int16_t *) b;
  1161. if(da[1] != db[1]) return da[1] - db[1];
  1162. else return da[0] - db[0];
  1163. }
  1164. static int deint(unsigned int a){
  1165. a &= 0x55555555; //0 1 2 3 4 5 6 7 8 9 A B C D E F
  1166. a += a & 0x11111111; // 01 23 45 67 89 AB CD EF
  1167. a += 3*(a & 0x0F0F0F0F);// 0123 4567 89AB CDEF
  1168. a += 15*(a & 0x00FF00FF);// 01234567 89ABCDEF
  1169. a +=255*(a & 0x0000FFFF);// 0123456789ABCDEF
  1170. return a>>15;
  1171. }
  1172. static void encode_subband_z0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1173. const int level= b->level;
  1174. const int w= b->width;
  1175. const int h= b->height;
  1176. int x, y, pos;
  1177. if(1){
  1178. int run=0;
  1179. int runs[w*h];
  1180. int run_index=0;
  1181. int count=0;
  1182. for(pos=0; ; pos++){
  1183. int x= deint(pos );
  1184. int y= deint(pos>>1);
  1185. int v, p=0, pr=0, pd=0;
  1186. int /*ll=0, */l=0, lt=0, t=0/*, rt=0*/;
  1187. if(x>=w || y>=h){
  1188. if(x>=w && y>=h)
  1189. break;
  1190. continue;
  1191. }
  1192. count++;
  1193. v= src[x + y*stride];
  1194. if(y){
  1195. t= src[x + (y-1)*stride];
  1196. if(x){
  1197. lt= src[x - 1 + (y-1)*stride];
  1198. }
  1199. if(x + 1 < w){
  1200. /*rt= src[x + 1 + (y-1)*stride]*/;
  1201. }
  1202. }
  1203. if(x){
  1204. l= src[x - 1 + y*stride];
  1205. /*if(x > 1){
  1206. if(orientation==1) ll= src[y + (x-2)*stride];
  1207. else ll= src[x - 2 + y*stride];
  1208. }*/
  1209. }
  1210. if(parent){
  1211. int px= x>>1;
  1212. int py= y>>1;
  1213. if(px<b->parent->width && py<b->parent->height){
  1214. p= parent[px + py*2*stride];
  1215. /*if(px+1<b->parent->width)
  1216. pr= parent[px + 1 + py*2*stride];
  1217. if(py+1<b->parent->height)
  1218. pd= parent[px + (py+1)*2*stride];*/
  1219. }
  1220. }
  1221. if(!(/*ll|*/l|lt|t|/*rt|*/p)){
  1222. if(v){
  1223. runs[run_index++]= run;
  1224. run=0;
  1225. }else{
  1226. run++;
  1227. }
  1228. }
  1229. }
  1230. assert(count==w*h);
  1231. runs[run_index++]= run;
  1232. run_index=0;
  1233. run= runs[run_index++];
  1234. put_symbol(&s->c, b->state[1], run, 0);
  1235. for(pos=0; ; pos++){
  1236. int x= deint(pos );
  1237. int y= deint(pos>>1);
  1238. int v, p=0, pr=0, pd=0;
  1239. int /*ll=0, */l=0, lt=0, t=0/*, rt=0*/;
  1240. if(x>=w || y>=h){
  1241. if(x>=w && y>=h)
  1242. break;
  1243. continue;
  1244. }
  1245. v= src[x + y*stride];
  1246. if(y){
  1247. t= src[x + (y-1)*stride];
  1248. if(x){
  1249. lt= src[x - 1 + (y-1)*stride];
  1250. }
  1251. if(x + 1 < w){
  1252. // rt= src[x + 1 + (y-1)*stride];
  1253. }
  1254. }
  1255. if(x){
  1256. l= src[x - 1 + y*stride];
  1257. /*if(x > 1){
  1258. if(orientation==1) ll= src[y + (x-2)*stride];
  1259. else ll= src[x - 2 + y*stride];
  1260. }*/
  1261. }
  1262. if(parent){
  1263. int px= x>>1;
  1264. int py= y>>1;
  1265. if(px<b->parent->width && py<b->parent->height){
  1266. p= parent[px + py*2*stride];
  1267. /* if(px+1<b->parent->width)
  1268. pr= parent[px + 1 + py*2*stride];
  1269. if(py+1<b->parent->height)
  1270. pd= parent[px + (py+1)*2*stride];*/
  1271. }
  1272. }
  1273. if(/*ll|*/l|lt|t|/*rt|*/p){
  1274. int context= av_log2(/*ABS(ll) + */2*(3*ABS(l) + ABS(lt) + 2*ABS(t) + /*ABS(rt) +*/ ABS(p)));
  1275. put_cabac(&s->c, &b->state[0][context], !!v);
  1276. }else{
  1277. if(!run){
  1278. run= runs[run_index++];
  1279. put_symbol(&s->c, b->state[1], run, 0);
  1280. assert(v);
  1281. }else{
  1282. run--;
  1283. assert(!v);
  1284. }
  1285. }
  1286. if(v){
  1287. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + /*ABS(rt) +*/ ABS(p));
  1288. put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0);
  1289. put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
  1290. }
  1291. }
  1292. }
  1293. }
  1294. static void encode_subband_bp(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1295. const int level= b->level;
  1296. const int w= b->width;
  1297. const int h= b->height;
  1298. int x, y;
  1299. #if 0
  1300. int plane;
  1301. for(plane=24; plane>=0; plane--){
  1302. int run=0;
  1303. int runs[w*h];
  1304. int run_index=0;
  1305. for(y=0; y<h; y++){
  1306. for(x=0; x<w; x++){
  1307. int v, lv, p=0;
  1308. int d=0, r=0, rd=0, ld=0;
  1309. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1310. v= src[x + y*stride];
  1311. if(y){
  1312. t= src[x + (y-1)*stride];
  1313. if(x){
  1314. lt= src[x - 1 + (y-1)*stride];
  1315. }
  1316. if(x + 1 < w){
  1317. rt= src[x + 1 + (y-1)*stride];
  1318. }
  1319. }
  1320. if(x){
  1321. l= src[x - 1 + y*stride];
  1322. /*if(x > 1){
  1323. if(orientation==1) ll= src[y + (x-2)*stride];
  1324. else ll= src[x - 2 + y*stride];
  1325. }*/
  1326. }
  1327. if(y+1<h){
  1328. d= src[x + (y+1)*stride];
  1329. if(x) ld= src[x - 1 + (y+1)*stride];
  1330. if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
  1331. }
  1332. if(x + 1 < w)
  1333. r= src[x + 1 + y*stride];
  1334. if(parent){
  1335. int px= x>>1;
  1336. int py= y>>1;
  1337. if(px<b->parent->width && py<b->parent->height)
  1338. p= parent[px + py*2*stride];
  1339. }
  1340. #define HIDE(c, plane) c= c>=0 ? c&((-1)<<(plane)) : -((-c)&((-1)<<(plane)));
  1341. lv=v;
  1342. HIDE( v, plane)
  1343. HIDE(lv, plane+1)
  1344. HIDE( p, plane)
  1345. HIDE( l, plane)
  1346. HIDE(lt, plane)
  1347. HIDE( t, plane)
  1348. HIDE(rt, plane)
  1349. HIDE( r, plane+1)
  1350. HIDE(rd, plane+1)
  1351. HIDE( d, plane+1)
  1352. HIDE(ld, plane+1)
  1353. if(!(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv)){
  1354. if(v){
  1355. runs[run_index++]= run;
  1356. run=0;
  1357. }else{
  1358. run++;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. runs[run_index++]= run;
  1364. run_index=0;
  1365. run= runs[run_index++];
  1366. put_symbol(&s->c, b->state[1], run, 0);
  1367. for(y=0; y<h; y++){
  1368. for(x=0; x<w; x++){
  1369. int v, p=0, lv;
  1370. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1371. int d=0, r=0, rd=0, ld=0;
  1372. v= src[x + y*stride];
  1373. if(y){
  1374. t= src[x + (y-1)*stride];
  1375. if(x){
  1376. lt= src[x - 1 + (y-1)*stride];
  1377. }
  1378. if(x + 1 < w){
  1379. rt= src[x + 1 + (y-1)*stride];
  1380. }
  1381. }
  1382. if(x){
  1383. l= src[x - 1 + y*stride];
  1384. /*if(x > 1){
  1385. if(orientation==1) ll= src[y + (x-2)*stride];
  1386. else ll= src[x - 2 + y*stride];
  1387. }*/
  1388. }
  1389. if(y+1<h){
  1390. d= src[x + (y+1)*stride];
  1391. if(x) ld= src[x - 1 + (y+1)*stride];
  1392. if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
  1393. }
  1394. if(x + 1 < w)
  1395. r= src[x + 1 + y*stride];
  1396. if(parent){
  1397. int px= x>>1;
  1398. int py= y>>1;
  1399. if(px<b->parent->width && py<b->parent->height)
  1400. p= parent[px + py*2*stride];
  1401. }
  1402. lv=v;
  1403. HIDE( v, plane)
  1404. HIDE(lv, plane+1)
  1405. HIDE( p, plane)
  1406. HIDE( l, plane)
  1407. HIDE(lt, plane)
  1408. HIDE( t, plane)
  1409. HIDE(rt, plane)
  1410. HIDE( r, plane+1)
  1411. HIDE(rd, plane+1)
  1412. HIDE( d, plane+1)
  1413. HIDE(ld, plane+1)
  1414. if(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv){
  1415. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)
  1416. +3*ABS(r) + ABS(rd) + 2*ABS(d) + ABS(ld));
  1417. if(lv) put_cabac(&s->c, &b->state[99][context + 8*(av_log2(ABS(lv))-plane)], !!(v-lv));
  1418. else put_cabac(&s->c, &b->state[ 0][context], !!v);
  1419. }else{
  1420. assert(!lv);
  1421. if(!run){
  1422. run= runs[run_index++];
  1423. put_symbol(&s->c, b->state[1], run, 0);
  1424. assert(v);
  1425. }else{
  1426. run--;
  1427. assert(!v);
  1428. }
  1429. }
  1430. if(v && !lv){
  1431. int context= clip(quant3b[l&0xFF] + quant3b[r&0xFF], -1,1)
  1432. + 3*clip(quant3b[t&0xFF] + quant3b[d&0xFF], -1,1);
  1433. put_cabac(&s->c, &b->state[0][16 + 1 + 3 + context], v<0);
  1434. }
  1435. }
  1436. }
  1437. }
  1438. return;
  1439. #endif
  1440. }
  1441. static void encode_subband_X(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1442. const int level= b->level;
  1443. const int w= b->width;
  1444. const int h= b->height;
  1445. int x, y;
  1446. #if 0
  1447. if(orientation==3 && parent && 0){
  1448. int16_t candidate[w*h][2];
  1449. uint8_t state[w*h];
  1450. int16_t boarder[3][w*h*4][2];
  1451. int16_t significant[w*h][2];
  1452. int candidate_count=0;
  1453. int boarder_count[3]={0,0,0};
  1454. int significant_count=0;
  1455. int rle_pos=0;
  1456. int v, last_v;
  1457. int primary= orientation==1;
  1458. memset(candidate, 0, sizeof(candidate));
  1459. memset(state, 0, sizeof(state));
  1460. memset(boarder, 0, sizeof(boarder));
  1461. for(y=0; y<h; y++){
  1462. for(x=0; x<w; x++){
  1463. if(parent[(x>>1) + (y>>1)*2*stride])
  1464. SVI(candidate, candidate_count, x, y)
  1465. }
  1466. }
  1467. for(;;){
  1468. while(candidate_count && !boarder_count[0] && !boarder_count[1] && !boarder_count[2]){
  1469. candidate_count--;
  1470. x= candidate[ candidate_count][0];
  1471. y= candidate[ candidate_count][1];
  1472. if(state[x + y*w])
  1473. continue;
  1474. state[x + y*w]= 1;
  1475. v= !!src[x + y*stride];
  1476. put_cabac(&s->c, &b->state[0][0], v);
  1477. if(v){
  1478. SVI(significant, significant_count, x,y)
  1479. if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
  1480. if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
  1481. if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
  1482. if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
  1483. if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
  1484. if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
  1485. if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
  1486. if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
  1487. }
  1488. }
  1489. while(!boarder_count[0] && !boarder_count[1] && !boarder_count[2] && rle_pos < w*h){
  1490. int run=0;
  1491. for(; rle_pos < w*h;){
  1492. x= rle_pos % w; //FIXME speed
  1493. y= rle_pos / w;
  1494. rle_pos++;
  1495. if(state[x + y*w])
  1496. continue;
  1497. state[x + y*w]= 1;
  1498. v= !!src[x + y*stride];
  1499. if(v){
  1500. put_symbol(&s->c, b->state[1], run, 0);
  1501. SVI(significant, significant_count, x,y)
  1502. if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
  1503. if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
  1504. if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
  1505. if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
  1506. if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
  1507. if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
  1508. if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
  1509. if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
  1510. break;
  1511. //FIXME note only right & down can be boarders
  1512. }
  1513. run++;
  1514. }
  1515. }
  1516. if(!boarder_count[0] && !boarder_count[1] && !boarder_count[2])
  1517. break;
  1518. while(boarder_count[0] || boarder_count[1] || boarder_count[2]){
  1519. int index;
  1520. if (boarder_count[ primary]) index= primary;
  1521. else if(boarder_count[1-primary]) index=1-primary;
  1522. else index=2;
  1523. boarder_count[index]--;
  1524. x= boarder[index][ boarder_count[index] ][0];
  1525. y= boarder[index][ boarder_count[index] ][1];
  1526. if(state[x + y*w]) //FIXME maybe check earlier
  1527. continue;
  1528. state[x + y*w]= 1;
  1529. v= !!src[x + y*stride];
  1530. put_cabac(&s->c, &b->state[0][index+1], v);
  1531. if(v){
  1532. SVI(significant, significant_count, x,y)
  1533. if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
  1534. if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
  1535. if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
  1536. if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
  1537. if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
  1538. if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
  1539. if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
  1540. if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
  1541. }
  1542. }
  1543. }
  1544. //FIXME sort significant coeffs maybe
  1545. if(1){
  1546. qsort(significant, significant_count, sizeof(int16_t[2]), sig_cmp);
  1547. }
  1548. last_v=1;
  1549. while(significant_count){
  1550. int context= 3 + quant7[last_v&0xFF]; //use significance of suroundings
  1551. significant_count--;
  1552. x= significant[significant_count][0];//FIXME try opposit direction
  1553. y= significant[significant_count][1];
  1554. v= src[x + y*stride];
  1555. put_symbol(&s->c, b->state[context + 2], v, 1); //FIXME try to avoid first bit, try this with the old code too!!
  1556. last_v= v;
  1557. }
  1558. }
  1559. #endif
  1560. }
  1561. static void encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1562. const int level= b->level;
  1563. const int w= b->width;
  1564. const int h= b->height;
  1565. int x, y;
  1566. if(1){
  1567. int run=0;
  1568. int runs[w*h];
  1569. int run_index=0;
  1570. for(y=0; y<h; y++){
  1571. for(x=0; x<w; x++){
  1572. int v, p=0;
  1573. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1574. v= src[x + y*stride];
  1575. if(y){
  1576. t= src[x + (y-1)*stride];
  1577. if(x){
  1578. lt= src[x - 1 + (y-1)*stride];
  1579. }
  1580. if(x + 1 < w){
  1581. rt= src[x + 1 + (y-1)*stride];
  1582. }
  1583. }
  1584. if(x){
  1585. l= src[x - 1 + y*stride];
  1586. /*if(x > 1){
  1587. if(orientation==1) ll= src[y + (x-2)*stride];
  1588. else ll= src[x - 2 + y*stride];
  1589. }*/
  1590. }
  1591. if(parent){
  1592. int px= x>>1;
  1593. int py= y>>1;
  1594. if(px<b->parent->width && py<b->parent->height)
  1595. p= parent[px + py*2*stride];
  1596. }
  1597. if(!(/*ll|*/l|lt|t|rt|p)){
  1598. if(v){
  1599. runs[run_index++]= run;
  1600. run=0;
  1601. }else{
  1602. run++;
  1603. }
  1604. }
  1605. }
  1606. }
  1607. runs[run_index++]= run;
  1608. run_index=0;
  1609. run= runs[run_index++];
  1610. put_symbol2(&s->c, b->state[1], run, 3);
  1611. for(y=0; y<h; y++){
  1612. for(x=0; x<w; x++){
  1613. int v, p=0;
  1614. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1615. v= src[x + y*stride];
  1616. if(y){
  1617. t= src[x + (y-1)*stride];
  1618. if(x){
  1619. lt= src[x - 1 + (y-1)*stride];
  1620. }
  1621. if(x + 1 < w){
  1622. rt= src[x + 1 + (y-1)*stride];
  1623. }
  1624. }
  1625. if(x){
  1626. l= src[x - 1 + y*stride];
  1627. /*if(x > 1){
  1628. if(orientation==1) ll= src[y + (x-2)*stride];
  1629. else ll= src[x - 2 + y*stride];
  1630. }*/
  1631. }
  1632. if(parent){
  1633. int px= x>>1;
  1634. int py= y>>1;
  1635. if(px<b->parent->width && py<b->parent->height)
  1636. p= parent[px + py*2*stride];
  1637. }
  1638. if(/*ll|*/l|lt|t|rt|p){
  1639. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1640. put_cabac(&s->c, &b->state[0][context], !!v);
  1641. }else{
  1642. if(!run){
  1643. run= runs[run_index++];
  1644. put_symbol2(&s->c, b->state[1], run, 3);
  1645. assert(v);
  1646. }else{
  1647. run--;
  1648. assert(!v);
  1649. }
  1650. }
  1651. if(v){
  1652. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1653. put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
  1654. put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
  1655. }
  1656. }
  1657. }
  1658. }
  1659. }
  1660. static void encode_subband_dzr(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1661. const int level= b->level;
  1662. const int w= b->width;
  1663. const int h= b->height;
  1664. int x, y;
  1665. if(1){
  1666. int run[16]={0};
  1667. int runs[16][w*h]; //FIXME do something about the size
  1668. int run_index[16]={0};
  1669. int positions[2][w];
  1670. int distances[2][w];
  1671. int dist_count=0;
  1672. int i;
  1673. for(y=0; y<h; y++){
  1674. int * pos = positions[ y&1];
  1675. int *last_pos = positions[(y&1)^1];
  1676. int * dist= distances[ y&1];
  1677. int *last_dist= distances[(y&1)^1];
  1678. int dist_index=0;
  1679. int last_dist_index=0;
  1680. for(x=0; x<w; x++){
  1681. int p=0, l=0, lt=0, t=0, rt=0;
  1682. int v= src[x + y*stride];
  1683. if(y){
  1684. t= src[x + (y-1)*stride];
  1685. if(x){
  1686. lt= src[x - 1 + (y-1)*stride];
  1687. }
  1688. if(x + 1 < w){
  1689. rt= src[x + 1 + (y-1)*stride];
  1690. }
  1691. }
  1692. if(x){
  1693. l= src[x - 1 + y*stride];
  1694. }
  1695. if(parent){
  1696. int px= x>>1;
  1697. int py= y>>1;
  1698. if(px<b->parent->width && py<b->parent->height)
  1699. p= parent[px + py*2*stride];
  1700. }
  1701. if(last_dist_index < dist_count && last_pos[last_dist_index] == x){
  1702. if(dist_index==0 || x - pos[dist_index-1] > dist[dist_index-1] - last_dist[last_dist_index]){
  1703. pos[dist_index]= x;
  1704. dist[dist_index++]= last_dist[last_dist_index];
  1705. }
  1706. last_dist_index++;
  1707. }
  1708. if(!(l|lt|t|rt|p)){
  1709. int cur_dist=w>>1;
  1710. int run_class;
  1711. if(last_dist_index < dist_count)
  1712. cur_dist= last_pos[last_dist_index] - x + y - last_dist[last_dist_index];
  1713. if(dist_index)
  1714. cur_dist= FFMIN(cur_dist, x - pos[dist_index-1] + y - dist[dist_index-1]);
  1715. assert(cur_dist>=2);
  1716. run_class= av_log2(cur_dist+62);
  1717. if(v){
  1718. runs[run_class][run_index[run_class]++]= run[run_class];
  1719. run[run_class]=0;
  1720. }else{
  1721. run[run_class]++;
  1722. }
  1723. }
  1724. if(v){
  1725. while(dist_index && x - pos[dist_index-1] <= y - dist[dist_index-1])
  1726. dist_index--;
  1727. pos[dist_index]= x;
  1728. dist[dist_index++]= y;
  1729. }
  1730. }
  1731. dist_count= dist_index;
  1732. }
  1733. for(i=0; i<12; i++){
  1734. runs[i][run_index[i]++]= run[i];
  1735. run_index[i]=0;
  1736. run[i]=0;
  1737. }
  1738. dist_count=0;
  1739. for(y=0; y<h; y++){
  1740. int * pos = positions[ y&1];
  1741. int *last_pos = positions[(y&1)^1];
  1742. int * dist= distances[ y&1];
  1743. int *last_dist= distances[(y&1)^1];
  1744. int dist_index=0;
  1745. int last_dist_index=0;
  1746. for(x=0; x<w; x++){
  1747. int p=0, l=0, lt=0, t=0, rt=0;
  1748. int v= src[x + y*stride];
  1749. if(y){
  1750. t= src[x + (y-1)*stride];
  1751. if(x){
  1752. lt= src[x - 1 + (y-1)*stride];
  1753. }
  1754. if(x + 1 < w){
  1755. rt= src[x + 1 + (y-1)*stride];
  1756. }
  1757. }
  1758. if(x){
  1759. l= src[x - 1 + y*stride];
  1760. }
  1761. if(parent){
  1762. int px= x>>1;
  1763. int py= y>>1;
  1764. if(px<b->parent->width && py<b->parent->height)
  1765. p= parent[px + py*2*stride];
  1766. }
  1767. if(last_dist_index < dist_count && last_pos[last_dist_index] == x){
  1768. if(dist_index==0 || x - pos[dist_index-1] > dist[dist_index-1] - last_dist[last_dist_index]){
  1769. pos[dist_index]= x;
  1770. dist[dist_index++]= last_dist[last_dist_index];
  1771. }
  1772. last_dist_index++;
  1773. }
  1774. if(l|lt|t|rt|p){
  1775. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1776. put_cabac(&s->c, &b->state[0][context], !!v);
  1777. }else{
  1778. int cur_dist=w>>1;
  1779. int run_class;
  1780. if(last_dist_index < dist_count)
  1781. cur_dist= last_pos[last_dist_index] - x + y - last_dist[last_dist_index];
  1782. if(dist_index)
  1783. cur_dist= FFMIN(cur_dist, x - pos[dist_index-1] + y - dist[dist_index-1]);
  1784. assert(cur_dist>=2);
  1785. assert(!dist_index || (pos[dist_index-1] >= 0 && pos[dist_index-1] <w));
  1786. assert(last_dist_index >= dist_count || (last_pos[last_dist_index] >= 0 && last_pos[last_dist_index] <w));
  1787. assert(!dist_index || dist[dist_index-1] <= y);
  1788. assert(last_dist_index >= dist_count || last_dist[last_dist_index] < y);
  1789. assert(cur_dist <= y + FFMAX(x, w-x-1));
  1790. run_class= av_log2(cur_dist+62);
  1791. if(!run_index[run_class]){
  1792. run[run_class]= runs[run_class][run_index[run_class]++];
  1793. put_symbol(&s->c, b->state[run_class+1], run[run_class], 0);
  1794. }
  1795. if(!run[run_class]){
  1796. run[run_class]= runs[run_class][run_index[run_class]++];
  1797. put_symbol(&s->c, b->state[run_class+1], run[run_class], 0);
  1798. assert(v);
  1799. }else{
  1800. run[run_class]--;
  1801. assert(!v);
  1802. }
  1803. }
  1804. if(v){
  1805. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1806. put_symbol(&s->c, b->state[context + 16], ABS(v)-1, 0);
  1807. put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
  1808. while(dist_index && x - pos[dist_index-1] <= y - dist[dist_index-1])
  1809. dist_index--;
  1810. pos[dist_index]= x;
  1811. dist[dist_index++]= y;
  1812. }
  1813. }
  1814. dist_count= dist_index;
  1815. }
  1816. }
  1817. }
  1818. static void encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1819. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  1820. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  1821. encode_subband_c0run(s, b, src, parent, stride, orientation);
  1822. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  1823. }
  1824. static inline void decode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1825. const int level= b->level;
  1826. const int w= b->width;
  1827. const int h= b->height;
  1828. int x,y;
  1829. START_TIMER
  1830. #if 0
  1831. for(y=0; y<b->height; y++)
  1832. memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
  1833. int plane;
  1834. for(plane=24; plane>=0; plane--){
  1835. int run;
  1836. run= get_symbol(&s->c, b->state[1], 0);
  1837. #define HIDE(c, plane) c= c>=0 ? c&((-1)<<(plane)) : -((-c)&((-1)<<(plane)));
  1838. for(y=0; y<h; y++){
  1839. for(x=0; x<w; x++){
  1840. int v, p=0, lv;
  1841. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1842. int d=0, r=0, rd=0, ld=0;
  1843. lv= src[x + y*stride];
  1844. if(y){
  1845. t= src[x + (y-1)*stride];
  1846. if(x){
  1847. lt= src[x - 1 + (y-1)*stride];
  1848. }
  1849. if(x + 1 < w){
  1850. rt= src[x + 1 + (y-1)*stride];
  1851. }
  1852. }
  1853. if(x){
  1854. l= src[x - 1 + y*stride];
  1855. /*if(x > 1){
  1856. if(orientation==1) ll= src[y + (x-2)*stride];
  1857. else ll= src[x - 2 + y*stride];
  1858. }*/
  1859. }
  1860. if(y+1<h){
  1861. d= src[x + (y+1)*stride];
  1862. if(x) ld= src[x - 1 + (y+1)*stride];
  1863. if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
  1864. }
  1865. if(x + 1 < w)
  1866. r= src[x + 1 + y*stride];
  1867. if(parent){
  1868. int px= x>>1;
  1869. int py= y>>1;
  1870. if(px<b->parent->width && py<b->parent->height)
  1871. p= parent[px + py*2*stride];
  1872. }
  1873. HIDE( p, plane)
  1874. if(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv){
  1875. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)
  1876. +3*ABS(r) + ABS(rd) + 2*ABS(d) + ABS(ld));
  1877. if(lv){
  1878. assert(context + 8*av_log2(ABS(lv)) < 512 - 100);
  1879. if(get_cabac(&s->c, &b->state[99][context + 8*(av_log2(ABS(lv))-plane)])){
  1880. if(lv<0) v= lv - (1<<plane);
  1881. else v= lv + (1<<plane);
  1882. }else
  1883. v=lv;
  1884. }else{
  1885. v= get_cabac(&s->c, &b->state[ 0][context]) << plane;
  1886. }
  1887. }else{
  1888. assert(!lv);
  1889. if(!run){
  1890. run= get_symbol(&s->c, b->state[1], 0);
  1891. v= 1<<plane;
  1892. }else{
  1893. run--;
  1894. v=0;
  1895. }
  1896. }
  1897. if(v && !lv){
  1898. int context= clip(quant3b[l&0xFF] + quant3b[r&0xFF], -1,1)
  1899. + 3*clip(quant3b[t&0xFF] + quant3b[d&0xFF], -1,1);
  1900. if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + context]))
  1901. v= -v;
  1902. }
  1903. src[x + y*stride]= v;
  1904. }
  1905. }
  1906. }
  1907. return;
  1908. #endif
  1909. #if 0
  1910. int tree[10][w*h]; //FIXME space waste ...
  1911. int treedim[10][2];
  1912. int lev;
  1913. const int max_level= av_log2(2*FFMAX(w,h)-1);
  1914. int w2=w, h2=h;
  1915. memset(tree, 0, sizeof(tree));
  1916. // assert(w%2==0 && h%2==0);
  1917. for(lev=max_level; lev>=0; lev--){
  1918. treedim[lev][0]= w2;
  1919. treedim[lev][1]= h2;
  1920. w2= (w2+1)>>1;
  1921. h2= (h2+1)>>1;
  1922. }
  1923. for(lev=0; lev<=max_level; lev++){
  1924. w2= treedim[lev][0];
  1925. h2= treedim[lev][1];
  1926. for(y=0; y<h2; y++){
  1927. for(x=0; x<w2; x++){
  1928. int l= 0, t=0;
  1929. int context;
  1930. if(lev && !tree[lev-1][x/2 + y/2*w])
  1931. continue;
  1932. if(x) l= tree[lev][x - 1 + y*w];
  1933. if(y) t= tree[lev][x + (y-1)*w];
  1934. context= lev + 8*(!!l) + 16*(!!t);
  1935. tree[lev][x + y*w]= get_cabac(&s->c, &b->state[98][context]);
  1936. }
  1937. }
  1938. }
  1939. if(1){
  1940. for(y=0; y<b->height; y++)
  1941. memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
  1942. for(y=0; y<h; y++){
  1943. for(x=0; x<w; x++){
  1944. int v, p=0;
  1945. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1946. if(y){
  1947. t= src[x + (y-1)*stride];
  1948. if(x){
  1949. lt= src[x - 1 + (y-1)*stride];
  1950. }
  1951. if(x + 1 < w){
  1952. rt= src[x + 1 + (y-1)*stride];
  1953. }
  1954. }
  1955. if(x){
  1956. l= src[x - 1 + y*stride];
  1957. /*if(x > 1){
  1958. if(orientation==1) ll= src[y + (x-2)*stride];
  1959. else ll= src[x - 2 + y*stride];
  1960. }*/
  1961. }
  1962. if(parent){
  1963. int px= x>>1;
  1964. int py= y>>1;
  1965. if(px<b->parent->width && py<b->parent->height)
  1966. p= parent[px + py*2*stride];
  1967. }
  1968. if(tree[max_level][x + y*w]){
  1969. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1970. v= get_symbol(&s->c, b->state[context + 2], 0) + 1;
  1971. if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
  1972. v= -v;
  1973. src[x + y*stride]= v;
  1974. }
  1975. }
  1976. }
  1977. if(level+1 == s->spatial_decomposition_count){
  1978. STOP_TIMER("decode_subband")
  1979. }
  1980. return;
  1981. }
  1982. #endif
  1983. if(1){
  1984. int run;
  1985. for(y=0; y<b->height; y++)
  1986. memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
  1987. run= get_symbol2(&s->c, b->state[1], 3);
  1988. for(y=0; y<h; y++){
  1989. for(x=0; x<w; x++){
  1990. int v, p=0;
  1991. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1992. if(y){
  1993. t= src[x + (y-1)*stride];
  1994. if(x){
  1995. lt= src[x - 1 + (y-1)*stride];
  1996. }
  1997. if(x + 1 < w){
  1998. rt= src[x + 1 + (y-1)*stride];
  1999. }
  2000. }
  2001. if(x){
  2002. l= src[x - 1 + y*stride];
  2003. /*if(x > 1){
  2004. if(orientation==1) ll= src[y + (x-2)*stride];
  2005. else ll= src[x - 2 + y*stride];
  2006. }*/
  2007. }
  2008. if(parent){
  2009. int px= x>>1;
  2010. int py= y>>1;
  2011. if(px<b->parent->width && py<b->parent->height)
  2012. p= parent[px + py*2*stride];
  2013. }
  2014. if(/*ll|*/l|lt|t|rt|p){
  2015. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  2016. v=get_cabac(&s->c, &b->state[0][context]);
  2017. }else{
  2018. if(!run){
  2019. run= get_symbol2(&s->c, b->state[1], 3);
  2020. //FIXME optimize this here
  2021. //FIXME try to store a more naive run
  2022. v=1;
  2023. }else{
  2024. run--;
  2025. v=0;
  2026. }
  2027. }
  2028. if(v){
  2029. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  2030. v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1;
  2031. if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
  2032. v= -v;
  2033. src[x + y*stride]= v;
  2034. }
  2035. }
  2036. }
  2037. if(level+1 == s->spatial_decomposition_count){
  2038. STOP_TIMER("decode_subband")
  2039. }
  2040. return;
  2041. }
  2042. }
  2043. static void reset_contexts(SnowContext *s){
  2044. int plane_index, level, orientation;
  2045. for(plane_index=0; plane_index<2; plane_index++){
  2046. for(level=0; level<s->spatial_decomposition_count; level++){
  2047. for(orientation=level ? 1:0; orientation<4; orientation++){
  2048. memset(s->plane[plane_index].band[level][orientation].state, 0, sizeof(s->plane[plane_index].band[level][orientation].state));
  2049. }
  2050. }
  2051. }
  2052. memset(s->mb_band.state, 0, sizeof(s->mb_band.state));
  2053. memset(s->mv_band[0].state, 0, sizeof(s->mv_band[0].state));
  2054. memset(s->mv_band[1].state, 0, sizeof(s->mv_band[1].state));
  2055. memset(s->header_state, 0, sizeof(s->header_state));
  2056. }
  2057. 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){
  2058. int x, y;
  2059. for(y=0; y < b_h+5; y++){
  2060. for(x=0; x < b_w; x++){
  2061. int a0= src[x + y*stride];
  2062. int a1= src[x + 1 + y*stride];
  2063. int a2= src[x + 2 + y*stride];
  2064. int a3= src[x + 3 + y*stride];
  2065. int a4= src[x + 4 + y*stride];
  2066. int a5= src[x + 5 + y*stride];
  2067. // int am= 9*(a1+a2) - (a0+a3);
  2068. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  2069. // int am= 18*(a2+a3) - 2*(a1+a4);
  2070. // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  2071. // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
  2072. // if(b_w==16) am= 8*(a1+a2);
  2073. if(dx<8) tmp[x + y*stride]= (32*a2*( 8-dx) + am* dx + 128)>>8;
  2074. else tmp[x + y*stride]= ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
  2075. /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
  2076. else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
  2077. else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
  2078. else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
  2079. }
  2080. }
  2081. for(y=0; y < b_h; y++){
  2082. for(x=0; x < b_w; x++){
  2083. int a0= tmp[x + y *stride];
  2084. int a1= tmp[x + (y + 1)*stride];
  2085. int a2= tmp[x + (y + 2)*stride];
  2086. int a3= tmp[x + (y + 3)*stride];
  2087. int a4= tmp[x + (y + 4)*stride];
  2088. int a5= tmp[x + (y + 5)*stride];
  2089. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  2090. // int am= 18*(a2+a3) - 2*(a1+a4);
  2091. /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  2092. int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
  2093. // if(b_w==16) am= 8*(a1+a2);
  2094. if(dy<8) dst[x + y*stride]= (32*a2*( 8-dy) + am* dy + 128)>>8;
  2095. else dst[x + y*stride]= ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
  2096. /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
  2097. else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
  2098. else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
  2099. else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
  2100. }
  2101. }
  2102. }
  2103. #define mcb(dx,dy,b_w)\
  2104. static void mc_block ## dx ## dy(uint8_t *dst, uint8_t *src, int stride){\
  2105. uint8_t tmp[stride*(b_w+5)];\
  2106. mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
  2107. }
  2108. mcb( 0, 0,16)
  2109. mcb( 4, 0,16)
  2110. mcb( 8, 0,16)
  2111. mcb(12, 0,16)
  2112. mcb( 0, 4,16)
  2113. mcb( 4, 4,16)
  2114. mcb( 8, 4,16)
  2115. mcb(12, 4,16)
  2116. mcb( 0, 8,16)
  2117. mcb( 4, 8,16)
  2118. mcb( 8, 8,16)
  2119. mcb(12, 8,16)
  2120. mcb( 0,12,16)
  2121. mcb( 4,12,16)
  2122. mcb( 8,12,16)
  2123. mcb(12,12,16)
  2124. #define mca(dx,dy,b_w)\
  2125. static void mc_block_hpel ## dx ## dy(uint8_t *dst, uint8_t *src, int stride, int h){\
  2126. uint8_t tmp[stride*(b_w+5)];\
  2127. assert(h==b_w);\
  2128. mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
  2129. }
  2130. mca( 0, 0,16)
  2131. mca( 8, 0,16)
  2132. mca( 0, 8,16)
  2133. mca( 8, 8,16)
  2134. 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){
  2135. uint8_t tmp[src_stride*(b_h+5)]; //FIXME move to context to gurantee alignment
  2136. int x,y;
  2137. if(s_x<0){
  2138. obmc -= s_x;
  2139. b_w += s_x;
  2140. s_x=0;
  2141. }else if(s_x + b_w > w){
  2142. b_w = w - s_x;
  2143. }
  2144. if(s_y<0){
  2145. obmc -= s_y*obmc_stride;
  2146. b_h += s_y;
  2147. s_y=0;
  2148. }else if(s_y + b_h> h){
  2149. b_h = h - s_y;
  2150. }
  2151. if(b_w<=0 || b_h<=0) return;
  2152. dst += s_x + s_y*dst_stride;
  2153. if(mb_type==1){
  2154. src += s_x + s_y*src_stride;
  2155. for(y=0; y < b_h; y++){
  2156. for(x=0; x < b_w; x++){
  2157. if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * 128 * (256/OBMC_MAX);
  2158. else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * 128 * (256/OBMC_MAX);
  2159. }
  2160. }
  2161. }else{
  2162. int dx= mv_x&15;
  2163. int dy= mv_y&15;
  2164. // int dxy= (mv_x&1) + 2*(mv_y&1);
  2165. s_x += (mv_x>>4) - 2;
  2166. s_y += (mv_y>>4) - 2;
  2167. src += s_x + s_y*src_stride;
  2168. //use dsputil
  2169. if( (unsigned)s_x >= w - b_w - 4
  2170. || (unsigned)s_y >= h - b_h - 4){
  2171. ff_emulated_edge_mc(tmp + 32, src, src_stride, b_w+5, b_h+5, s_x, s_y, w, h);
  2172. src= tmp + 32;
  2173. }
  2174. if(mb_type==0){
  2175. mc_block(tmp, src, tmp + 64+8, src_stride, b_w, b_h, dx, dy);
  2176. }else{
  2177. int sum=0;
  2178. for(y=0; y < b_h; y++){
  2179. for(x=0; x < b_w; x++){
  2180. sum += src[x+ y*src_stride];
  2181. }
  2182. }
  2183. sum= (sum + b_h*b_w/2) / (b_h*b_w);
  2184. for(y=0; y < b_h; y++){
  2185. for(x=0; x < b_w; x++){
  2186. tmp[x + y*src_stride]= sum;
  2187. }
  2188. }
  2189. }
  2190. for(y=0; y < b_h; y++){
  2191. for(x=0; x < b_w; x++){
  2192. if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
  2193. else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
  2194. }
  2195. }
  2196. }
  2197. }
  2198. static void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
  2199. Plane *p= &s->plane[plane_index];
  2200. const int mb_w= s->mb_band.width;
  2201. const int mb_h= s->mb_band.height;
  2202. const int mb_stride= s->mb_band.stride;
  2203. int x, y, mb_x, mb_y;
  2204. int scale = plane_index ? s->mv_scale : 2*s->mv_scale;
  2205. int block_w = plane_index ? 8 : 16;
  2206. uint8_t *obmc = plane_index ? obmc16 : obmc32;
  2207. int obmc_stride= plane_index ? 16 : 32;
  2208. int ref_stride= s->last_picture.linesize[plane_index];
  2209. uint8_t *ref = s->last_picture.data[plane_index];
  2210. int w= p->width;
  2211. int h= p->height;
  2212. if(s->avctx->debug&512){
  2213. for(y=0; y<h; y++){
  2214. for(x=0; x<w; x++){
  2215. if(add) buf[x + y*w]+= 128*256;
  2216. else buf[x + y*w]-= 128*256;
  2217. }
  2218. }
  2219. return;
  2220. }
  2221. for(mb_y=-1; mb_y<=mb_h; mb_y++){
  2222. for(mb_x=-1; mb_x<=mb_w; mb_x++){
  2223. int index= clip(mb_x, 0, mb_w-1) + clip(mb_y, 0, mb_h-1)*mb_stride;
  2224. add_xblock(buf, ref, obmc,
  2225. block_w*mb_x - block_w/2,
  2226. block_w*mb_y - block_w/2,
  2227. 2*block_w, 2*block_w,
  2228. s->mv_band[0].buf[index]*scale, s->mv_band[1].buf[index]*scale,
  2229. w, h,
  2230. w, ref_stride, obmc_stride,
  2231. s->mb_band.buf[index], add);
  2232. }
  2233. }
  2234. }
  2235. static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
  2236. const int level= b->level;
  2237. const int w= b->width;
  2238. const int h= b->height;
  2239. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  2240. const int qmul= qexp[qlog&7]<<(qlog>>3);
  2241. int x,y, thres1, thres2;
  2242. START_TIMER
  2243. assert(QROOT==8);
  2244. bias= bias ? 0 : (3*qmul)>>3;
  2245. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  2246. thres2= 2*thres1;
  2247. if(!bias){
  2248. for(y=0; y<h; y++){
  2249. for(x=0; x<w; x++){
  2250. int i= src[x + y*stride];
  2251. if((unsigned)(i+thres1) > thres2){
  2252. if(i>=0){
  2253. i<<= QEXPSHIFT;
  2254. i/= qmul; //FIXME optimize
  2255. src[x + y*stride]= i;
  2256. }else{
  2257. i= -i;
  2258. i<<= QEXPSHIFT;
  2259. i/= qmul; //FIXME optimize
  2260. src[x + y*stride]= -i;
  2261. }
  2262. }else
  2263. src[x + y*stride]= 0;
  2264. }
  2265. }
  2266. }else{
  2267. for(y=0; y<h; y++){
  2268. for(x=0; x<w; x++){
  2269. int i= src[x + y*stride];
  2270. if((unsigned)(i+thres1) > thres2){
  2271. if(i>=0){
  2272. i<<= QEXPSHIFT;
  2273. i= (i + bias) / qmul; //FIXME optimize
  2274. src[x + y*stride]= i;
  2275. }else{
  2276. i= -i;
  2277. i<<= QEXPSHIFT;
  2278. i= (i + bias) / qmul; //FIXME optimize
  2279. src[x + y*stride]= -i;
  2280. }
  2281. }else
  2282. src[x + y*stride]= 0;
  2283. }
  2284. }
  2285. }
  2286. if(level+1 == s->spatial_decomposition_count){
  2287. // STOP_TIMER("quantize")
  2288. }
  2289. }
  2290. static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
  2291. const int level= b->level;
  2292. const int w= b->width;
  2293. const int h= b->height;
  2294. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  2295. const int qmul= qexp[qlog&7]<<(qlog>>3);
  2296. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2297. int x,y;
  2298. assert(QROOT==8);
  2299. for(y=0; y<h; y++){
  2300. for(x=0; x<w; x++){
  2301. int i= src[x + y*stride];
  2302. if(i<0){
  2303. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2304. }else if(i>0){
  2305. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2306. }
  2307. }
  2308. }
  2309. }
  2310. static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  2311. const int w= b->width;
  2312. const int h= b->height;
  2313. int x,y;
  2314. for(y=h-1; y>=0; y--){
  2315. for(x=w-1; x>=0; x--){
  2316. int i= x + y*stride;
  2317. if(x){
  2318. if(use_median){
  2319. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2320. else src[i] -= src[i - 1];
  2321. }else{
  2322. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2323. else src[i] -= src[i - 1];
  2324. }
  2325. }else{
  2326. if(y) src[i] -= src[i - stride];
  2327. }
  2328. }
  2329. }
  2330. }
  2331. static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  2332. const int w= b->width;
  2333. const int h= b->height;
  2334. int x,y;
  2335. for(y=0; y<h; y++){
  2336. for(x=0; x<w; x++){
  2337. int i= x + y*stride;
  2338. if(x){
  2339. if(use_median){
  2340. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2341. else src[i] += src[i - 1];
  2342. }else{
  2343. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2344. else src[i] += src[i - 1];
  2345. }
  2346. }else{
  2347. if(y) src[i] += src[i - stride];
  2348. }
  2349. }
  2350. }
  2351. }
  2352. static void encode_header(SnowContext *s){
  2353. int plane_index, level, orientation;
  2354. put_cabac(&s->c, s->header_state, s->keyframe); // state clearing stuff?
  2355. if(s->keyframe){
  2356. put_symbol(&s->c, s->header_state, s->version, 0);
  2357. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  2358. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  2359. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2360. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  2361. put_symbol(&s->c, s->header_state, s->b_width, 0);
  2362. put_symbol(&s->c, s->header_state, s->b_height, 0);
  2363. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  2364. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  2365. put_cabac(&s->c, s->header_state, s->spatial_scalability);
  2366. // put_cabac(&s->c, s->header_state, s->rate_scalability);
  2367. for(plane_index=0; plane_index<2; plane_index++){
  2368. for(level=0; level<s->spatial_decomposition_count; level++){
  2369. for(orientation=level ? 1:0; orientation<4; orientation++){
  2370. if(orientation==2) continue;
  2371. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  2372. }
  2373. }
  2374. }
  2375. }
  2376. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
  2377. put_symbol(&s->c, s->header_state, s->qlog, 1);
  2378. put_symbol(&s->c, s->header_state, s->mv_scale, 0);
  2379. put_symbol(&s->c, s->header_state, s->qbias, 1);
  2380. }
  2381. static int decode_header(SnowContext *s){
  2382. int plane_index, level, orientation;
  2383. s->keyframe= get_cabac(&s->c, s->header_state);
  2384. if(s->keyframe){
  2385. s->version= get_symbol(&s->c, s->header_state, 0);
  2386. if(s->version>0){
  2387. av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
  2388. return -1;
  2389. }
  2390. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2391. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2392. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2393. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  2394. s->b_width= get_symbol(&s->c, s->header_state, 0);
  2395. s->b_height= get_symbol(&s->c, s->header_state, 0);
  2396. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  2397. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  2398. s->spatial_scalability= get_cabac(&s->c, s->header_state);
  2399. // s->rate_scalability= get_cabac(&s->c, s->header_state);
  2400. for(plane_index=0; plane_index<3; plane_index++){
  2401. for(level=0; level<s->spatial_decomposition_count; level++){
  2402. for(orientation=level ? 1:0; orientation<4; orientation++){
  2403. int q;
  2404. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  2405. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  2406. else q= get_symbol(&s->c, s->header_state, 1);
  2407. s->plane[plane_index].band[level][orientation].qlog= q;
  2408. }
  2409. }
  2410. }
  2411. }
  2412. s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2413. if(s->spatial_decomposition_type > 2){
  2414. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  2415. return -1;
  2416. }
  2417. s->qlog= get_symbol(&s->c, s->header_state, 1);
  2418. s->mv_scale= get_symbol(&s->c, s->header_state, 0);
  2419. s->qbias= get_symbol(&s->c, s->header_state, 1);
  2420. return 0;
  2421. }
  2422. static int common_init(AVCodecContext *avctx){
  2423. SnowContext *s = avctx->priv_data;
  2424. int width, height;
  2425. int level, orientation, plane_index, dec;
  2426. s->avctx= avctx;
  2427. dsputil_init(&s->dsp, avctx);
  2428. #define mcf(dx,dy)\
  2429. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  2430. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  2431. mc_block ## dx ## dy;
  2432. mcf( 0, 0)
  2433. mcf( 4, 0)
  2434. mcf( 8, 0)
  2435. mcf(12, 0)
  2436. mcf( 0, 4)
  2437. mcf( 4, 4)
  2438. mcf( 8, 4)
  2439. mcf(12, 4)
  2440. mcf( 0, 8)
  2441. mcf( 4, 8)
  2442. mcf( 8, 8)
  2443. mcf(12, 8)
  2444. mcf( 0,12)
  2445. mcf( 4,12)
  2446. mcf( 8,12)
  2447. mcf(12,12)
  2448. #define mcfh(dx,dy)\
  2449. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  2450. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  2451. mc_block_hpel ## dx ## dy;
  2452. mcfh(0, 0)
  2453. mcfh(8, 0)
  2454. mcfh(0, 8)
  2455. mcfh(8, 8)
  2456. dec= s->spatial_decomposition_count= 5;
  2457. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  2458. s->chroma_h_shift= 1; //FIXME XXX
  2459. s->chroma_v_shift= 1;
  2460. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  2461. s->b_width = (s->avctx->width +(1<<dec)-1)>>dec;
  2462. s->b_height= (s->avctx->height+(1<<dec)-1)>>dec;
  2463. s->spatial_dwt_buffer= av_mallocz(s->b_width*s->b_height*sizeof(DWTELEM)<<(2*dec));
  2464. s->pred_buffer= av_mallocz(s->b_width*s->b_height*sizeof(DWTELEM)<<(2*dec));
  2465. s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  2466. for(plane_index=0; plane_index<3; plane_index++){
  2467. int w= s->avctx->width;
  2468. int h= s->avctx->height;
  2469. if(plane_index){
  2470. w>>= s->chroma_h_shift;
  2471. h>>= s->chroma_v_shift;
  2472. }
  2473. s->plane[plane_index].width = w;
  2474. s->plane[plane_index].height= h;
  2475. av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
  2476. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  2477. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2478. SubBand *b= &s->plane[plane_index].band[level][orientation];
  2479. b->buf= s->spatial_dwt_buffer;
  2480. b->level= level;
  2481. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  2482. b->width = (w + !(orientation&1))>>1;
  2483. b->height= (h + !(orientation>1))>>1;
  2484. if(orientation&1) b->buf += (w+1)>>1;
  2485. if(orientation>1) b->buf += b->stride>>1;
  2486. if(level)
  2487. b->parent= &s->plane[plane_index].band[level-1][orientation];
  2488. }
  2489. w= (w+1)>>1;
  2490. h= (h+1)>>1;
  2491. }
  2492. }
  2493. //FIXME init_subband() ?
  2494. s->mb_band.stride= s->mv_band[0].stride= s->mv_band[1].stride=
  2495. s->mb_band.width = s->mv_band[0].width = s->mv_band[1].width = (s->avctx->width + 15)>>4;
  2496. s->mb_band.height= s->mv_band[0].height= s->mv_band[1].height= (s->avctx->height+ 15)>>4;
  2497. s->mb_band .buf= av_mallocz(s->mb_band .stride * s->mb_band .height*sizeof(DWTELEM));
  2498. s->mv_band[0].buf= av_mallocz(s->mv_band[0].stride * s->mv_band[0].height*sizeof(DWTELEM));
  2499. s->mv_band[1].buf= av_mallocz(s->mv_band[1].stride * s->mv_band[1].height*sizeof(DWTELEM));
  2500. reset_contexts(s);
  2501. /*
  2502. width= s->width= avctx->width;
  2503. height= s->height= avctx->height;
  2504. assert(width && height);
  2505. */
  2506. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  2507. return 0;
  2508. }
  2509. static void calculate_vissual_weight(SnowContext *s, Plane *p){
  2510. int width = p->width;
  2511. int height= p->height;
  2512. int i, level, orientation, x, y;
  2513. for(level=0; level<s->spatial_decomposition_count; level++){
  2514. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2515. SubBand *b= &p->band[level][orientation];
  2516. DWTELEM *buf= b->buf;
  2517. int64_t error=0;
  2518. memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
  2519. buf[b->width/2 + b->height/2*b->stride]= 256*256;
  2520. spatial_idwt(s, s->spatial_dwt_buffer, width, height, width);
  2521. for(y=0; y<height; y++){
  2522. for(x=0; x<width; x++){
  2523. int64_t d= s->spatial_dwt_buffer[x + y*width];
  2524. error += d*d;
  2525. }
  2526. }
  2527. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  2528. av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
  2529. }
  2530. }
  2531. }
  2532. static int encode_init(AVCodecContext *avctx)
  2533. {
  2534. SnowContext *s = avctx->priv_data;
  2535. int i;
  2536. int level, orientation, plane_index;
  2537. if(avctx->strict_std_compliance >= 0){
  2538. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  2539. "use vstrict=-1 to use it anyway\n");
  2540. return -1;
  2541. }
  2542. common_init(avctx);
  2543. s->version=0;
  2544. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  2545. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2546. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2547. s->mb_type = av_mallocz((s->mb_band.width+1)*s->mb_band.height*sizeof(int16_t));
  2548. s->mb_mean = av_mallocz((s->mb_band.width+1)*s->mb_band.height*sizeof(int8_t ));
  2549. s->dummy = av_mallocz((s->mb_band.width+1)*s->mb_band.height*sizeof(int32_t));
  2550. h263_encode_init(&s->m); //mv_penalty
  2551. for(plane_index=0; plane_index<3; plane_index++){
  2552. calculate_vissual_weight(s, &s->plane[plane_index]);
  2553. }
  2554. avctx->coded_frame= &s->current_picture;
  2555. switch(avctx->pix_fmt){
  2556. // case PIX_FMT_YUV444P:
  2557. // case PIX_FMT_YUV422P:
  2558. case PIX_FMT_YUV420P:
  2559. case PIX_FMT_GRAY8:
  2560. // case PIX_FMT_YUV411P:
  2561. // case PIX_FMT_YUV410P:
  2562. s->colorspace_type= 0;
  2563. break;
  2564. /* case PIX_FMT_RGBA32:
  2565. s->colorspace= 1;
  2566. break;*/
  2567. default:
  2568. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  2569. return -1;
  2570. }
  2571. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  2572. s->chroma_h_shift= 1;
  2573. s->chroma_v_shift= 1;
  2574. return 0;
  2575. }
  2576. static int frame_start(SnowContext *s){
  2577. AVFrame tmp;
  2578. if(s->keyframe)
  2579. reset_contexts(s);
  2580. tmp= s->last_picture;
  2581. s->last_picture= s->current_picture;
  2582. s->current_picture= tmp;
  2583. s->current_picture.reference= 1;
  2584. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  2585. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  2586. return -1;
  2587. }
  2588. return 0;
  2589. }
  2590. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  2591. SnowContext *s = avctx->priv_data;
  2592. CABACContext * const c= &s->c;
  2593. AVFrame *pict = data;
  2594. const int width= s->avctx->width;
  2595. const int height= s->avctx->height;
  2596. int used_count= 0;
  2597. int log2_threshold, level, orientation, plane_index, i;
  2598. ff_init_cabac_encoder(c, buf, buf_size);
  2599. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2600. s->input_picture = *pict;
  2601. memset(s->header_state, 0, sizeof(s->header_state));
  2602. s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  2603. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  2604. s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
  2605. //<64 >60
  2606. s->qlog += 61;
  2607. for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){
  2608. s->mb_band.buf[i]= s->keyframe;
  2609. }
  2610. frame_start(s);
  2611. if(pict->pict_type == P_TYPE){
  2612. int block_width = (width +15)>>4;
  2613. int block_height= (height+15)>>4;
  2614. int stride= s->current_picture.linesize[0];
  2615. uint8_t *src_plane= s->input_picture.data[0];
  2616. int src_stride= s->input_picture.linesize[0];
  2617. int x,y;
  2618. assert(s->current_picture.data[0]);
  2619. assert(s->last_picture.data[0]);
  2620. s->m.avctx= s->avctx;
  2621. s->m.current_picture.data[0]= s->current_picture.data[0];
  2622. s->m. last_picture.data[0]= s-> last_picture.data[0];
  2623. s->m. new_picture.data[0]= s-> input_picture.data[0];
  2624. s->m.current_picture_ptr= &s->m.current_picture;
  2625. s->m. last_picture_ptr= &s->m. last_picture;
  2626. s->m.linesize=
  2627. s->m. last_picture.linesize[0]=
  2628. s->m. new_picture.linesize[0]=
  2629. s->m.current_picture.linesize[0]= stride;
  2630. s->m.width = width;
  2631. s->m.height= height;
  2632. s->m.mb_width = block_width;
  2633. s->m.mb_height= block_height;
  2634. s->m.mb_stride= s->m.mb_width+1;
  2635. s->m.b8_stride= 2*s->m.mb_width+1;
  2636. s->m.f_code=1;
  2637. s->m.pict_type= pict->pict_type;
  2638. s->m.me_method= s->avctx->me_method;
  2639. s->m.me.scene_change_score=0;
  2640. s->m.flags= s->avctx->flags;
  2641. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  2642. s->m.out_format= FMT_H263;
  2643. s->m.unrestricted_mv= 1;
  2644. s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else
  2645. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  2646. s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  2647. if(!s->motion_val8){
  2648. s->motion_val8 = av_mallocz(s->m.b8_stride*block_height*2*2*sizeof(int16_t));
  2649. s->motion_val16= av_mallocz(s->m.mb_stride*block_height*2*sizeof(int16_t));
  2650. }
  2651. s->m.mb_type= s->mb_type;
  2652. //dummies, to avoid segfaults
  2653. s->m.current_picture.mb_mean = s->mb_mean;
  2654. s->m.current_picture.mb_var = (int16_t*)s->dummy;
  2655. s->m.current_picture.mc_mb_var= (int16_t*)s->dummy;
  2656. s->m.current_picture.mb_type = s->dummy;
  2657. s->m.current_picture.motion_val[0]= s->motion_val8;
  2658. s->m.p_mv_table= s->motion_val16;
  2659. s->m.dsp= s->dsp; //move
  2660. ff_init_me(&s->m);
  2661. s->m.me.pre_pass=1;
  2662. s->m.me.dia_size= s->avctx->pre_dia_size;
  2663. s->m.first_slice_line=1;
  2664. for(y= block_height-1; y >= 0; y--) {
  2665. uint8_t src[stride*16];
  2666. s->m.new_picture.data[0]= src - y*16*stride; //ugly
  2667. s->m.mb_y= y;
  2668. for(i=0; i<16 && i + 16*y<height; i++){
  2669. memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
  2670. for(x=width; x<16*block_width; x++)
  2671. src[i*stride+x]= src[i*stride+x-1];
  2672. }
  2673. for(; i<16 && i + 16*y<16*block_height; i++)
  2674. memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
  2675. for(x=block_width-1; x >=0 ;x--) {
  2676. s->m.mb_x= x;
  2677. ff_init_block_index(&s->m);
  2678. ff_update_block_index(&s->m);
  2679. ff_pre_estimate_p_frame_motion(&s->m, x, y);
  2680. }
  2681. s->m.first_slice_line=0;
  2682. }
  2683. s->m.me.pre_pass=0;
  2684. s->m.me.dia_size= s->avctx->dia_size;
  2685. s->m.first_slice_line=1;
  2686. for (y = 0; y < block_height; y++) {
  2687. uint8_t src[stride*16];
  2688. s->m.new_picture.data[0]= src - y*16*stride; //ugly
  2689. s->m.mb_y= y;
  2690. assert(width <= stride);
  2691. assert(width <= 16*block_width);
  2692. for(i=0; i<16 && i + 16*y<height; i++){
  2693. memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
  2694. for(x=width; x<16*block_width; x++)
  2695. src[i*stride+x]= src[i*stride+x-1];
  2696. }
  2697. for(; i<16 && i + 16*y<16*block_height; i++)
  2698. memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
  2699. for (x = 0; x < block_width; x++) {
  2700. int mb_xy= x + y*(s->mb_band.stride);
  2701. s->m.mb_x= x;
  2702. ff_init_block_index(&s->m);
  2703. ff_update_block_index(&s->m);
  2704. ff_estimate_p_frame_motion(&s->m, x, y);
  2705. s->mb_band .buf[mb_xy]= (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER)
  2706. ? 0 : 2;
  2707. s->mv_band[0].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][0];
  2708. s->mv_band[1].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][1];
  2709. if(s->mb_band .buf[x + y*(s->mb_band.stride)]==2 && 0){
  2710. int dc0=128, dc1=128, dc, dc2, dir;
  2711. int offset= (s->avctx->flags & CODEC_FLAG_QPEL) ? 64 : 32;
  2712. dc =s->mb_mean[x + y *s->m.mb_stride ];
  2713. if(x) dc0=s->mb_mean[x + y *s->m.mb_stride - 1];
  2714. if(y) dc1=s->mb_mean[x + (y-1)*s->m.mb_stride ];
  2715. dc2= (dc0+dc1)>>1;
  2716. #if 0
  2717. if (ABS(dc0 - dc) < ABS(dc1 - dc) && ABS(dc0 - dc) < ABS(dc2 - dc))
  2718. dir= 1;
  2719. else if(ABS(dc0 - dc) >=ABS(dc1 - dc) && ABS(dc1 - dc) < ABS(dc2 - dc))
  2720. dir=-1;
  2721. else
  2722. dir=0;
  2723. #endif
  2724. if(ABS(dc0 - dc) < ABS(dc1 - dc) && x){
  2725. s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + y*(s->mb_band.stride)-1] - offset;
  2726. s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + y*(s->mb_band.stride)-1];
  2727. s->mb_mean[x + y *s->m.mb_stride ]= dc0;
  2728. }else if(y){
  2729. s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + (y-1)*(s->mb_band.stride)];
  2730. s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + (y-1)*(s->mb_band.stride)] - offset;
  2731. s->mb_mean[x + y *s->m.mb_stride ]= dc1;
  2732. }
  2733. }
  2734. // s->mb_band .buf[x + y*(s->mb_band.stride)]=1; //FIXME intra only test
  2735. }
  2736. s->m.first_slice_line=0;
  2737. }
  2738. assert(s->m.pict_type == P_TYPE);
  2739. if(s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  2740. s->m.pict_type=
  2741. pict->pict_type =I_TYPE;
  2742. for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){
  2743. s->mb_band.buf[i]= 1;
  2744. s->mv_band[0].buf[i]=
  2745. s->mv_band[1].buf[i]= 0;
  2746. }
  2747. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  2748. }
  2749. }
  2750. s->m.first_slice_line=1;
  2751. s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
  2752. encode_header(s);
  2753. decorrelate(s, &s->mb_band , s->mb_band .buf, s->mb_band .stride, 0, 1);
  2754. decorrelate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 0, 1);
  2755. decorrelate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 0 ,1);
  2756. encode_subband(s, &s->mb_band , s->mb_band .buf, NULL, s->mb_band .stride, 0);
  2757. encode_subband(s, &s->mv_band[0], s->mv_band[0].buf, NULL, s->mv_band[0].stride, 0);
  2758. encode_subband(s, &s->mv_band[1], s->mv_band[1].buf, NULL, s->mv_band[1].stride, 0);
  2759. //FIXME avoid this
  2760. correlate(s, &s->mb_band , s->mb_band .buf, s->mb_band .stride, 1, 1);
  2761. correlate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 1, 1);
  2762. correlate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 1, 1);
  2763. for(plane_index=0; plane_index<3; plane_index++){
  2764. Plane *p= &s->plane[plane_index];
  2765. int w= p->width;
  2766. int h= p->height;
  2767. int x, y;
  2768. int bits= put_bits_count(&s->c.pb);
  2769. //FIXME optimize
  2770. #if QPRED
  2771. memset(s->pred_buffer, 0, sizeof(DWTELEM)*w*h);
  2772. predict_plane(s, s->pred_buffer, plane_index, 1);
  2773. spatial_dwt(s, s->pred_buffer, w, h, w);
  2774. for(level=0; level<s->spatial_decomposition_count; level++){
  2775. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2776. SubBand *b= &p->band[level][orientation];
  2777. int delta= ((int)s->pred_buffer - (int)s->spatial_dwt_buffer)/sizeof(DWTELEM);
  2778. quantize (s, b, b->buf + delta, b->stride, s->qbias);
  2779. dequantize(s, b, b->buf + delta, b->stride);
  2780. }
  2781. }
  2782. for(y=0; y<h; y++){
  2783. for(x=0; x<w; x++){
  2784. s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;
  2785. }
  2786. }
  2787. spatial_dwt(s, s->spatial_dwt_buffer, w, h, w);
  2788. for(y=0; y<h; y++){
  2789. for(x=0; x<w; x++){
  2790. s->spatial_dwt_buffer[y*w + x]-= s->pred_buffer[y*w + x];
  2791. }
  2792. }
  2793. #else
  2794. if(pict->data[plane_index]) //FIXME gray hack
  2795. for(y=0; y<h; y++){
  2796. for(x=0; x<w; x++){
  2797. s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;
  2798. }
  2799. }
  2800. predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
  2801. spatial_dwt(s, s->spatial_dwt_buffer, w, h, w);
  2802. #endif
  2803. for(level=0; level<s->spatial_decomposition_count; level++){
  2804. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2805. SubBand *b= &p->band[level][orientation];
  2806. quantize(s, b, b->buf, b->stride, s->qbias);
  2807. if(orientation==0)
  2808. decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
  2809. encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  2810. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  2811. if(orientation==0)
  2812. correlate(s, b, b->buf, b->stride, 1, 0);
  2813. }
  2814. }
  2815. // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
  2816. for(level=0; level<s->spatial_decomposition_count; level++){
  2817. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2818. SubBand *b= &p->band[level][orientation];
  2819. dequantize(s, b, b->buf, b->stride);
  2820. }
  2821. }
  2822. #if QPRED
  2823. for(y=0; y<h; y++){
  2824. for(x=0; x<w; x++){
  2825. s->spatial_dwt_buffer[y*w + x]+= s->pred_buffer[y*w + x];
  2826. }
  2827. }
  2828. spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
  2829. #else
  2830. spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
  2831. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2832. #endif
  2833. //FIXME optimize
  2834. for(y=0; y<h; y++){
  2835. for(x=0; x<w; x++){
  2836. int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
  2837. if(v&(~255)) v= ~(v>>31);
  2838. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;
  2839. }
  2840. }
  2841. if(s->avctx->flags&CODEC_FLAG_PSNR){
  2842. int64_t error= 0;
  2843. if(pict->data[plane_index]) //FIXME gray hack
  2844. for(y=0; y<h; y++){
  2845. for(x=0; x<w; x++){
  2846. int d= s->spatial_dwt_buffer[y*w + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x]*256;
  2847. error += d*d;
  2848. }
  2849. }
  2850. error= (error + 128*256)>>16;
  2851. s->avctx->error[plane_index] += error;
  2852. s->avctx->error[3] += error;
  2853. }
  2854. }
  2855. if(s->last_picture.data[0])
  2856. avctx->release_buffer(avctx, &s->last_picture);
  2857. emms_c();
  2858. return put_cabac_terminate(c, 1);
  2859. }
  2860. static void common_end(SnowContext *s){
  2861. av_freep(&s->spatial_dwt_buffer);
  2862. av_freep(&s->mb_band.buf);
  2863. av_freep(&s->mv_band[0].buf);
  2864. av_freep(&s->mv_band[1].buf);
  2865. av_freep(&s->m.me.scratchpad);
  2866. av_freep(&s->m.me.map);
  2867. av_freep(&s->m.me.score_map);
  2868. av_freep(&s->mb_type);
  2869. av_freep(&s->mb_mean);
  2870. av_freep(&s->dummy);
  2871. av_freep(&s->motion_val8);
  2872. av_freep(&s->motion_val16);
  2873. }
  2874. static int encode_end(AVCodecContext *avctx)
  2875. {
  2876. SnowContext *s = avctx->priv_data;
  2877. common_end(s);
  2878. return 0;
  2879. }
  2880. static int decode_init(AVCodecContext *avctx)
  2881. {
  2882. // SnowContext *s = avctx->priv_data;
  2883. common_init(avctx);
  2884. return 0;
  2885. }
  2886. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  2887. SnowContext *s = avctx->priv_data;
  2888. CABACContext * const c= &s->c;
  2889. const int width= s->avctx->width;
  2890. const int height= s->avctx->height;
  2891. int bytes_read;
  2892. AVFrame *picture = data;
  2893. int log2_threshold, level, orientation, plane_index;
  2894. /* no supplementary picture */
  2895. if (buf_size == 0)
  2896. return 0;
  2897. ff_init_cabac_decoder(c, buf, buf_size);
  2898. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2899. memset(s->header_state, 0, sizeof(s->header_state));
  2900. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  2901. decode_header(s);
  2902. frame_start(s);
  2903. //keyframe flag dupliaction mess FIXME
  2904. if(avctx->debug&FF_DEBUG_PICT_INFO)
  2905. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  2906. decode_subband(s, &s->mb_band , s->mb_band .buf, NULL, s->mb_band .stride, 0);
  2907. decode_subband(s, &s->mv_band[0], s->mv_band[0].buf, NULL, s->mv_band[0].stride, 0);
  2908. decode_subband(s, &s->mv_band[1], s->mv_band[1].buf, NULL, s->mv_band[1].stride, 0);
  2909. correlate(s, &s->mb_band , s->mb_band .buf, s->mb_band .stride, 1, 1);
  2910. correlate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 1, 1);
  2911. correlate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 1, 1);
  2912. for(plane_index=0; plane_index<3; plane_index++){
  2913. Plane *p= &s->plane[plane_index];
  2914. int w= p->width;
  2915. int h= p->height;
  2916. int x, y;
  2917. if(s->avctx->debug&2048){
  2918. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  2919. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2920. for(y=0; y<h; y++){
  2921. for(x=0; x<w; x++){
  2922. int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
  2923. if(v&(~255)) v= ~(v>>31);
  2924. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  2925. }
  2926. }
  2927. }
  2928. for(level=0; level<s->spatial_decomposition_count; level++){
  2929. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2930. SubBand *b= &p->band[level][orientation];
  2931. decode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  2932. if(orientation==0)
  2933. correlate(s, b, b->buf, b->stride, 1, 0);
  2934. }
  2935. }
  2936. if(!(s->avctx->debug&1024))
  2937. for(level=0; level<s->spatial_decomposition_count; level++){
  2938. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2939. SubBand *b= &p->band[level][orientation];
  2940. dequantize(s, b, b->buf, b->stride);
  2941. }
  2942. }
  2943. #if QPRED
  2944. memset(s->pred_buffer, 0, sizeof(DWTELEM)*w*h);
  2945. predict_plane(s, s->pred_buffer, plane_index, 1);
  2946. spatial_dwt(s, s->pred_buffer, w, h, w);
  2947. for(level=0; level<s->spatial_decomposition_count; level++){
  2948. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2949. SubBand *b= &p->band[level][orientation];
  2950. int delta= ((int)s->pred_buffer - (int)s->spatial_dwt_buffer)/sizeof(DWTELEM);
  2951. quantize (s, b, b->buf + delta, b->stride, s->qbias);
  2952. dequantize(s, b, b->buf + delta, b->stride);
  2953. }
  2954. }
  2955. for(y=0; y<h; y++){
  2956. for(x=0; x<w; x++){
  2957. s->spatial_dwt_buffer[y*w + x]+= s->pred_buffer[y*w + x];
  2958. }
  2959. }
  2960. spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
  2961. #else
  2962. spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
  2963. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2964. #endif
  2965. //FIXME optimize
  2966. for(y=0; y<h; y++){
  2967. for(x=0; x<w; x++){
  2968. int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
  2969. if(v&(~255)) v= ~(v>>31);
  2970. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;
  2971. }
  2972. }
  2973. }
  2974. emms_c();
  2975. if(s->last_picture.data[0])
  2976. avctx->release_buffer(avctx, &s->last_picture);
  2977. if(!(s->avctx->debug&2048))
  2978. *picture= s->current_picture;
  2979. else
  2980. *picture= s->mconly_picture;
  2981. *data_size = sizeof(AVFrame);
  2982. bytes_read= get_cabac_terminate(c);
  2983. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n");
  2984. return bytes_read;
  2985. }
  2986. static int decode_end(AVCodecContext *avctx)
  2987. {
  2988. SnowContext *s = avctx->priv_data;
  2989. common_end(s);
  2990. return 0;
  2991. }
  2992. AVCodec snow_decoder = {
  2993. "snow",
  2994. CODEC_TYPE_VIDEO,
  2995. CODEC_ID_SNOW,
  2996. sizeof(SnowContext),
  2997. decode_init,
  2998. NULL,
  2999. decode_end,
  3000. decode_frame,
  3001. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  3002. NULL
  3003. };
  3004. AVCodec snow_encoder = {
  3005. "snow",
  3006. CODEC_TYPE_VIDEO,
  3007. CODEC_ID_SNOW,
  3008. sizeof(SnowContext),
  3009. encode_init,
  3010. encode_frame,
  3011. encode_end,
  3012. };
  3013. #if 0
  3014. #undef malloc
  3015. #undef free
  3016. #undef printf
  3017. int main(){
  3018. int width=256;
  3019. int height=256;
  3020. int buffer[2][width*height];
  3021. SnowContext s;
  3022. int i;
  3023. s.spatial_decomposition_count=6;
  3024. s.spatial_decomposition_type=1;
  3025. printf("testing 5/3 DWT\n");
  3026. for(i=0; i<width*height; i++)
  3027. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  3028. spatial_dwt(&s, buffer[0], width, height, width);
  3029. spatial_idwt(&s, buffer[0], width, height, width);
  3030. for(i=0; i<width*height; i++)
  3031. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  3032. printf("testing 9/7 DWT\n");
  3033. s.spatial_decomposition_type=0;
  3034. for(i=0; i<width*height; i++)
  3035. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  3036. spatial_dwt(&s, buffer[0], width, height, width);
  3037. spatial_idwt(&s, buffer[0], width, height, width);
  3038. for(i=0; i<width*height; i++)
  3039. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  3040. printf("testing AC coder\n");
  3041. memset(s.header_state, 0, sizeof(s.header_state));
  3042. ff_init_cabac_encoder(&s.c, buffer[0], 256*256);
  3043. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3044. for(i=-256; i<256; i++){
  3045. START_TIMER
  3046. put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
  3047. STOP_TIMER("put_symbol")
  3048. }
  3049. put_cabac_terminate(&s.c, 1);
  3050. memset(s.header_state, 0, sizeof(s.header_state));
  3051. ff_init_cabac_decoder(&s.c, buffer[0], 256*256);
  3052. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3053. for(i=-256; i<256; i++){
  3054. int j;
  3055. START_TIMER
  3056. j= get_symbol(&s.c, s.header_state, 1);
  3057. STOP_TIMER("get_symbol")
  3058. if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
  3059. }
  3060. {
  3061. int level, orientation, x, y;
  3062. int64_t errors[8][4];
  3063. int64_t g=0;
  3064. memset(errors, 0, sizeof(errors));
  3065. s.spatial_decomposition_count=3;
  3066. s.spatial_decomposition_type=0;
  3067. for(level=0; level<s.spatial_decomposition_count; level++){
  3068. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3069. int w= width >> (s.spatial_decomposition_count-level);
  3070. int h= height >> (s.spatial_decomposition_count-level);
  3071. int stride= width << (s.spatial_decomposition_count-level);
  3072. DWTELEM *buf= buffer[0];
  3073. int64_t error=0;
  3074. if(orientation&1) buf+=w;
  3075. if(orientation>1) buf+=stride>>1;
  3076. memset(buffer[0], 0, sizeof(int)*width*height);
  3077. buf[w/2 + h/2*stride]= 256*256;
  3078. spatial_idwt(&s, buffer[0], width, height, width);
  3079. for(y=0; y<height; y++){
  3080. for(x=0; x<width; x++){
  3081. int64_t d= buffer[0][x + y*width];
  3082. error += d*d;
  3083. if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
  3084. }
  3085. if(ABS(height/2-y)<9 && level==2) printf("\n");
  3086. }
  3087. error= (int)(sqrt(error)+0.5);
  3088. errors[level][orientation]= error;
  3089. if(g) g=ff_gcd(g, error);
  3090. else g= error;
  3091. }
  3092. }
  3093. printf("static int const visual_weight[][4]={\n");
  3094. for(level=0; level<s.spatial_decomposition_count; level++){
  3095. printf(" {");
  3096. for(orientation=0; orientation<4; orientation++){
  3097. printf("%8lld,", errors[level][orientation]/g);
  3098. }
  3099. printf("},\n");
  3100. }
  3101. printf("};\n");
  3102. {
  3103. int level=2;
  3104. int orientation=3;
  3105. int w= width >> (s.spatial_decomposition_count-level);
  3106. int h= height >> (s.spatial_decomposition_count-level);
  3107. int stride= width << (s.spatial_decomposition_count-level);
  3108. DWTELEM *buf= buffer[0];
  3109. int64_t error=0;
  3110. buf+=w;
  3111. buf+=stride>>1;
  3112. memset(buffer[0], 0, sizeof(int)*width*height);
  3113. #if 1
  3114. for(y=0; y<height; y++){
  3115. for(x=0; x<width; x++){
  3116. int tab[4]={0,2,3,1};
  3117. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  3118. }
  3119. }
  3120. spatial_dwt(&s, buffer[0], width, height, width);
  3121. #else
  3122. for(y=0; y<h; y++){
  3123. for(x=0; x<w; x++){
  3124. buf[x + y*stride ]=169;
  3125. buf[x + y*stride-w]=64;
  3126. }
  3127. }
  3128. spatial_idwt(&s, buffer[0], width, height, width);
  3129. #endif
  3130. for(y=0; y<height; y++){
  3131. for(x=0; x<width; x++){
  3132. int64_t d= buffer[0][x + y*width];
  3133. error += d*d;
  3134. if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
  3135. }
  3136. if(ABS(height/2-y)<9) printf("\n");
  3137. }
  3138. }
  3139. }
  3140. return 0;
  3141. }
  3142. #endif