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.

3255 lines
111KB

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