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.

3053 lines
106KB

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