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.

3573 lines
120KB

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