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.

4675 lines
166KB

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