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.

3528 lines
118KB

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