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.

4568 lines
161KB

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