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.

3939 lines
133KB

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