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.

4781 lines
169KB

  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){
  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)
  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)
  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_buffered(SnowContext *s, slice_buffer * sb, DWTELEM *old_dst, uint8_t *dst8, uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int plane_index){
  2271. DWTELEM * dst = NULL;
  2272. const int b_width = s->b_width << s->block_max_depth;
  2273. const int b_height= s->b_height << s->block_max_depth;
  2274. const int b_stride= b_width;
  2275. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  2276. BlockNode *rt= lt+1;
  2277. BlockNode *lb= lt+b_stride;
  2278. BlockNode *rb= lb+1;
  2279. uint8_t *block[4];
  2280. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  2281. uint8_t tmp[src_stride*7*MB_SIZE]; //FIXME align
  2282. uint8_t *ptmp;
  2283. int x,y;
  2284. if(b_x<0){
  2285. lt= rt;
  2286. lb= rb;
  2287. }else if(b_x + 1 >= b_width){
  2288. rt= lt;
  2289. rb= lb;
  2290. }
  2291. if(b_y<0){
  2292. lt= lb;
  2293. rt= rb;
  2294. }else if(b_y + 1 >= b_height){
  2295. lb= lt;
  2296. rb= rt;
  2297. }
  2298. if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
  2299. obmc -= src_x;
  2300. b_w += src_x;
  2301. src_x=0;
  2302. }else if(src_x + b_w > w){
  2303. b_w = w - src_x;
  2304. }
  2305. if(src_y<0){
  2306. obmc -= src_y*obmc_stride;
  2307. b_h += src_y;
  2308. src_y=0;
  2309. }else if(src_y + b_h> h){
  2310. b_h = h - src_y;
  2311. }
  2312. if(b_w<=0 || b_h<=0) return;
  2313. assert(src_stride > 2*MB_SIZE + 5);
  2314. // old_dst += src_x + src_y*dst_stride;
  2315. dst8+= src_x + src_y*src_stride;
  2316. // src += src_x + src_y*src_stride;
  2317. ptmp= tmp + 3*tmp_step;
  2318. block[0]= ptmp;
  2319. ptmp+=tmp_step;
  2320. pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  2321. if(same_block(lt, rt)){
  2322. block[1]= block[0];
  2323. }else{
  2324. block[1]= ptmp;
  2325. ptmp+=tmp_step;
  2326. pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  2327. }
  2328. if(same_block(lt, lb)){
  2329. block[2]= block[0];
  2330. }else if(same_block(rt, lb)){
  2331. block[2]= block[1];
  2332. }else{
  2333. block[2]= ptmp;
  2334. ptmp+=tmp_step;
  2335. pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  2336. }
  2337. if(same_block(lt, rb) ){
  2338. block[3]= block[0];
  2339. }else if(same_block(rt, rb)){
  2340. block[3]= block[1];
  2341. }else if(same_block(lb, rb)){
  2342. block[3]= block[2];
  2343. }else{
  2344. block[3]= ptmp;
  2345. pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  2346. }
  2347. #if 0
  2348. for(y=0; y<b_h; y++){
  2349. for(x=0; x<b_w; x++){
  2350. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  2351. if(add) dst[x + y*dst_stride] += v;
  2352. else dst[x + y*dst_stride] -= v;
  2353. }
  2354. }
  2355. for(y=0; y<b_h; y++){
  2356. uint8_t *obmc2= obmc + (obmc_stride>>1);
  2357. for(x=0; x<b_w; x++){
  2358. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  2359. if(add) dst[x + y*dst_stride] += v;
  2360. else dst[x + y*dst_stride] -= v;
  2361. }
  2362. }
  2363. for(y=0; y<b_h; y++){
  2364. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2365. for(x=0; x<b_w; x++){
  2366. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  2367. if(add) dst[x + y*dst_stride] += v;
  2368. else dst[x + y*dst_stride] -= v;
  2369. }
  2370. }
  2371. for(y=0; y<b_h; y++){
  2372. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2373. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2374. for(x=0; x<b_w; x++){
  2375. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  2376. if(add) dst[x + y*dst_stride] += v;
  2377. else dst[x + y*dst_stride] -= v;
  2378. }
  2379. }
  2380. #else
  2381. {
  2382. START_TIMER
  2383. s->dsp.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  2384. STOP_TIMER("Inner add y block")
  2385. }
  2386. #endif
  2387. }
  2388. //FIXME name clenup (b_w, block_w, b_width stuff)
  2389. static always_inline void add_yblock(SnowContext *s, 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){
  2390. const int b_width = s->b_width << s->block_max_depth;
  2391. const int b_height= s->b_height << s->block_max_depth;
  2392. const int b_stride= b_width;
  2393. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  2394. BlockNode *rt= lt+1;
  2395. BlockNode *lb= lt+b_stride;
  2396. BlockNode *rb= lb+1;
  2397. uint8_t *block[4];
  2398. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  2399. uint8_t tmp[src_stride*7*MB_SIZE]; //FIXME align
  2400. uint8_t *ptmp;
  2401. int x,y;
  2402. if(b_x<0){
  2403. lt= rt;
  2404. lb= rb;
  2405. }else if(b_x + 1 >= b_width){
  2406. rt= lt;
  2407. rb= lb;
  2408. }
  2409. if(b_y<0){
  2410. lt= lb;
  2411. rt= rb;
  2412. }else if(b_y + 1 >= b_height){
  2413. lb= lt;
  2414. rb= rt;
  2415. }
  2416. if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
  2417. obmc -= src_x;
  2418. b_w += src_x;
  2419. if(!offset_dst)
  2420. dst -= src_x;
  2421. src_x=0;
  2422. }else if(src_x + b_w > w){
  2423. b_w = w - src_x;
  2424. }
  2425. if(src_y<0){
  2426. obmc -= src_y*obmc_stride;
  2427. b_h += src_y;
  2428. if(!offset_dst)
  2429. dst -= src_y*dst_stride;
  2430. src_y=0;
  2431. }else if(src_y + b_h> h){
  2432. b_h = h - src_y;
  2433. }
  2434. if(b_w<=0 || b_h<=0) return;
  2435. assert(src_stride > 2*MB_SIZE + 5);
  2436. if(offset_dst)
  2437. dst += src_x + src_y*dst_stride;
  2438. dst8+= src_x + src_y*src_stride;
  2439. // src += src_x + src_y*src_stride;
  2440. ptmp= tmp + 3*tmp_step;
  2441. block[0]= ptmp;
  2442. ptmp+=tmp_step;
  2443. pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  2444. if(same_block(lt, rt)){
  2445. block[1]= block[0];
  2446. }else{
  2447. block[1]= ptmp;
  2448. ptmp+=tmp_step;
  2449. pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  2450. }
  2451. if(same_block(lt, lb)){
  2452. block[2]= block[0];
  2453. }else if(same_block(rt, lb)){
  2454. block[2]= block[1];
  2455. }else{
  2456. block[2]= ptmp;
  2457. ptmp+=tmp_step;
  2458. pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  2459. }
  2460. if(same_block(lt, rb) ){
  2461. block[3]= block[0];
  2462. }else if(same_block(rt, rb)){
  2463. block[3]= block[1];
  2464. }else if(same_block(lb, rb)){
  2465. block[3]= block[2];
  2466. }else{
  2467. block[3]= ptmp;
  2468. pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  2469. }
  2470. #if 0
  2471. for(y=0; y<b_h; y++){
  2472. for(x=0; x<b_w; x++){
  2473. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  2474. if(add) dst[x + y*dst_stride] += v;
  2475. else dst[x + y*dst_stride] -= v;
  2476. }
  2477. }
  2478. for(y=0; y<b_h; y++){
  2479. uint8_t *obmc2= obmc + (obmc_stride>>1);
  2480. for(x=0; x<b_w; x++){
  2481. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  2482. if(add) dst[x + y*dst_stride] += v;
  2483. else dst[x + y*dst_stride] -= v;
  2484. }
  2485. }
  2486. for(y=0; y<b_h; y++){
  2487. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2488. for(x=0; x<b_w; x++){
  2489. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  2490. if(add) dst[x + y*dst_stride] += v;
  2491. else dst[x + y*dst_stride] -= v;
  2492. }
  2493. }
  2494. for(y=0; y<b_h; y++){
  2495. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2496. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2497. for(x=0; x<b_w; x++){
  2498. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  2499. if(add) dst[x + y*dst_stride] += v;
  2500. else dst[x + y*dst_stride] -= v;
  2501. }
  2502. }
  2503. #else
  2504. for(y=0; y<b_h; y++){
  2505. //FIXME ugly missue of obmc_stride
  2506. uint8_t *obmc1= obmc + y*obmc_stride;
  2507. uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2508. uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2509. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2510. for(x=0; x<b_w; x++){
  2511. int v= obmc1[x] * block[3][x + y*src_stride]
  2512. +obmc2[x] * block[2][x + y*src_stride]
  2513. +obmc3[x] * block[1][x + y*src_stride]
  2514. +obmc4[x] * block[0][x + y*src_stride];
  2515. v <<= 8 - LOG2_OBMC_MAX;
  2516. if(FRAC_BITS != 8){
  2517. v += 1<<(7 - FRAC_BITS);
  2518. v >>= 8 - FRAC_BITS;
  2519. }
  2520. if(add){
  2521. v += dst[x + y*dst_stride];
  2522. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2523. if(v&(~255)) v= ~(v>>31);
  2524. dst8[x + y*src_stride] = v;
  2525. }else{
  2526. dst[x + y*dst_stride] -= v;
  2527. }
  2528. }
  2529. }
  2530. #endif
  2531. }
  2532. static always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, DWTELEM * old_buffer, int plane_index, int add, int mb_y){
  2533. Plane *p= &s->plane[plane_index];
  2534. const int mb_w= s->b_width << s->block_max_depth;
  2535. const int mb_h= s->b_height << s->block_max_depth;
  2536. int x, y, mb_x;
  2537. int block_size = MB_SIZE >> s->block_max_depth;
  2538. int block_w = plane_index ? block_size/2 : block_size;
  2539. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2540. int obmc_stride= plane_index ? block_size : 2*block_size;
  2541. int ref_stride= s->current_picture.linesize[plane_index];
  2542. uint8_t *dst8= s->current_picture.data[plane_index];
  2543. int w= p->width;
  2544. int h= p->height;
  2545. START_TIMER
  2546. if(s->keyframe || (s->avctx->debug&512)){
  2547. if(mb_y==mb_h)
  2548. return;
  2549. if(add){
  2550. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++)
  2551. {
  2552. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2553. DWTELEM * line = sb->line[y];
  2554. for(x=0; x<w; x++)
  2555. {
  2556. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2557. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2558. v >>= FRAC_BITS;
  2559. if(v&(~255)) v= ~(v>>31);
  2560. dst8[x + y*ref_stride]= v;
  2561. }
  2562. }
  2563. }else{
  2564. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++)
  2565. {
  2566. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2567. DWTELEM * line = sb->line[y];
  2568. for(x=0; x<w; x++)
  2569. {
  2570. line[x] -= 128 << FRAC_BITS;
  2571. // buf[x + y*w]-= 128<<FRAC_BITS;
  2572. }
  2573. }
  2574. }
  2575. return;
  2576. }
  2577. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2578. START_TIMER
  2579. add_yblock_buffered(s, sb, old_buffer, dst8, obmc,
  2580. block_w*mb_x - block_w/2,
  2581. block_w*mb_y - block_w/2,
  2582. block_w, block_w,
  2583. w, h,
  2584. w, ref_stride, obmc_stride,
  2585. mb_x - 1, mb_y - 1,
  2586. add, plane_index);
  2587. STOP_TIMER("add_yblock")
  2588. }
  2589. STOP_TIMER("predict_slice")
  2590. }
  2591. static always_inline void predict_slice(SnowContext *s, DWTELEM *buf, int plane_index, int add, int mb_y){
  2592. Plane *p= &s->plane[plane_index];
  2593. const int mb_w= s->b_width << s->block_max_depth;
  2594. const int mb_h= s->b_height << s->block_max_depth;
  2595. int x, y, mb_x;
  2596. int block_size = MB_SIZE >> s->block_max_depth;
  2597. int block_w = plane_index ? block_size/2 : block_size;
  2598. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2599. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2600. int ref_stride= s->current_picture.linesize[plane_index];
  2601. uint8_t *dst8= s->current_picture.data[plane_index];
  2602. int w= p->width;
  2603. int h= p->height;
  2604. START_TIMER
  2605. if(s->keyframe || (s->avctx->debug&512)){
  2606. if(mb_y==mb_h)
  2607. return;
  2608. if(add){
  2609. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2610. for(x=0; x<w; x++){
  2611. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2612. v >>= FRAC_BITS;
  2613. if(v&(~255)) v= ~(v>>31);
  2614. dst8[x + y*ref_stride]= v;
  2615. }
  2616. }
  2617. }else{
  2618. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2619. for(x=0; x<w; x++){
  2620. buf[x + y*w]-= 128<<FRAC_BITS;
  2621. }
  2622. }
  2623. }
  2624. return;
  2625. }
  2626. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2627. START_TIMER
  2628. add_yblock(s, buf, dst8, obmc,
  2629. block_w*mb_x - block_w/2,
  2630. block_w*mb_y - block_w/2,
  2631. block_w, block_w,
  2632. w, h,
  2633. w, ref_stride, obmc_stride,
  2634. mb_x - 1, mb_y - 1,
  2635. add, 1, plane_index);
  2636. STOP_TIMER("add_yblock")
  2637. }
  2638. STOP_TIMER("predict_slice")
  2639. }
  2640. static always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
  2641. const int mb_h= s->b_height << s->block_max_depth;
  2642. int mb_y;
  2643. for(mb_y=0; mb_y<=mb_h; mb_y++)
  2644. predict_slice(s, buf, plane_index, add, mb_y);
  2645. }
  2646. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2647. int i, x2, y2;
  2648. Plane *p= &s->plane[plane_index];
  2649. const int block_size = MB_SIZE >> s->block_max_depth;
  2650. const int block_w = plane_index ? block_size/2 : block_size;
  2651. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2652. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2653. const int ref_stride= s->current_picture.linesize[plane_index];
  2654. uint8_t *src= s-> input_picture.data[plane_index];
  2655. DWTELEM *dst= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2656. const int b_stride = s->b_width << s->block_max_depth;
  2657. const int w= p->width;
  2658. const int h= p->height;
  2659. int index= mb_x + mb_y*b_stride;
  2660. BlockNode *b= &s->block[index];
  2661. BlockNode backup= *b;
  2662. int ab=0;
  2663. int aa=0;
  2664. b->type|= BLOCK_INTRA;
  2665. b->color[plane_index]= 0;
  2666. memset(dst, 0, obmc_stride*obmc_stride*sizeof(DWTELEM));
  2667. for(i=0; i<4; i++){
  2668. int mb_x2= mb_x + (i &1) - 1;
  2669. int mb_y2= mb_y + (i>>1) - 1;
  2670. int x= block_w*mb_x2 + block_w/2;
  2671. int y= block_w*mb_y2 + block_w/2;
  2672. add_yblock(s, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  2673. x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  2674. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
  2675. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  2676. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
  2677. int obmc_v= obmc[index];
  2678. int d;
  2679. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  2680. if(x<0) obmc_v += obmc[index + block_w];
  2681. if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
  2682. if(x+block_w>w) obmc_v += obmc[index - block_w];
  2683. //FIXME precalc this or simplify it somehow else
  2684. d = -dst[index] + (1<<(FRAC_BITS-1));
  2685. dst[index] = d;
  2686. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  2687. aa += obmc_v * obmc_v; //FIXME precalclate this
  2688. }
  2689. }
  2690. }
  2691. *b= backup;
  2692. return clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we shouldnt need cliping
  2693. }
  2694. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  2695. const int b_stride = s->b_width << s->block_max_depth;
  2696. const int b_height = s->b_height<< s->block_max_depth;
  2697. int index= x + y*b_stride;
  2698. BlockNode *b = &s->block[index];
  2699. BlockNode *left = x ? &s->block[index-1] : &null_block;
  2700. BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  2701. BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  2702. BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  2703. int dmx, dmy;
  2704. // int mx_context= av_log2(2*ABS(left->mx - top->mx));
  2705. // int my_context= av_log2(2*ABS(left->my - top->my));
  2706. if(x<0 || x>=b_stride || y>=b_height)
  2707. return 0;
  2708. /*
  2709. 1 0 0
  2710. 01X 1-2 1
  2711. 001XX 3-6 2-3
  2712. 0001XXX 7-14 4-7
  2713. 00001XXXX 15-30 8-15
  2714. */
  2715. //FIXME try accurate rate
  2716. //FIXME intra and inter predictors if surrounding blocks arent the same type
  2717. if(b->type & BLOCK_INTRA){
  2718. return 3+2*( av_log2(2*ABS(left->color[0] - b->color[0]))
  2719. + av_log2(2*ABS(left->color[1] - b->color[1]))
  2720. + av_log2(2*ABS(left->color[2] - b->color[2])));
  2721. }else{
  2722. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  2723. dmx-= b->mx;
  2724. dmy-= b->my;
  2725. return 2*(1 + av_log2(2*ABS(dmx)) //FIXME kill the 2* can be merged in lambda
  2726. + av_log2(2*ABS(dmy))
  2727. + av_log2(2*b->ref));
  2728. }
  2729. }
  2730. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
  2731. Plane *p= &s->plane[plane_index];
  2732. const int block_size = MB_SIZE >> s->block_max_depth;
  2733. const int block_w = plane_index ? block_size/2 : block_size;
  2734. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2735. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2736. const int ref_stride= s->current_picture.linesize[plane_index];
  2737. uint8_t *dst= s->current_picture.data[plane_index];
  2738. uint8_t *src= s-> input_picture.data[plane_index];
  2739. DWTELEM *pred= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2740. uint8_t cur[ref_stride*2*MB_SIZE]; //FIXME alignment
  2741. uint8_t tmp[ref_stride*(2*MB_SIZE+5)];
  2742. const int b_stride = s->b_width << s->block_max_depth;
  2743. const int b_height = s->b_height<< s->block_max_depth;
  2744. const int w= p->width;
  2745. const int h= p->height;
  2746. int distortion;
  2747. int rate= 0;
  2748. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2749. int sx= block_w*mb_x - block_w/2;
  2750. int sy= block_w*mb_y - block_w/2;
  2751. int x0= FFMAX(0,-sx);
  2752. int y0= FFMAX(0,-sy);
  2753. int x1= FFMIN(block_w*2, w-sx);
  2754. int y1= FFMIN(block_w*2, h-sy);
  2755. int i,x,y;
  2756. 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);
  2757. for(y=y0; y<y1; y++){
  2758. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  2759. const DWTELEM *pred1 = pred + y*obmc_stride;
  2760. uint8_t *cur1 = cur + y*ref_stride;
  2761. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  2762. for(x=x0; x<x1; x++){
  2763. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  2764. v = (v + pred1[x]) >> FRAC_BITS;
  2765. if(v&(~255)) v= ~(v>>31);
  2766. dst1[x] = v;
  2767. }
  2768. }
  2769. /* copy the regions where obmc[] = (uint8_t)256 */
  2770. if(LOG2_OBMC_MAX == 8
  2771. && (mb_x == 0 || mb_x == b_stride-1)
  2772. && (mb_y == 0 || mb_y == b_height-1)){
  2773. if(mb_x == 0)
  2774. x1 = block_w;
  2775. else
  2776. x0 = block_w;
  2777. if(mb_y == 0)
  2778. y1 = block_w;
  2779. else
  2780. y0 = block_w;
  2781. for(y=y0; y<y1; y++)
  2782. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  2783. }
  2784. if(block_w==16){
  2785. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  2786. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  2787. /* FIXME cmps overlap but don't cover the wavelet's whole support,
  2788. * so improving the score of one block is not strictly guaranteed to
  2789. * improve the score of the whole frame, so iterative motion est
  2790. * doesn't always converge. */
  2791. if(s->avctx->me_cmp == FF_CMP_W97)
  2792. distortion = w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2793. else if(s->avctx->me_cmp == FF_CMP_W53)
  2794. distortion = w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2795. else{
  2796. distortion = 0;
  2797. for(i=0; i<4; i++){
  2798. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  2799. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  2800. }
  2801. }
  2802. }else{
  2803. assert(block_w==8);
  2804. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  2805. }
  2806. if(plane_index==0){
  2807. for(i=0; i<4; i++){
  2808. /* ..RRr
  2809. * .RXx.
  2810. * rxx..
  2811. */
  2812. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  2813. }
  2814. if(mb_x == b_stride-2)
  2815. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  2816. }
  2817. return distortion + rate*penalty_factor;
  2818. }
  2819. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2820. int i, y2;
  2821. Plane *p= &s->plane[plane_index];
  2822. const int block_size = MB_SIZE >> s->block_max_depth;
  2823. const int block_w = plane_index ? block_size/2 : block_size;
  2824. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2825. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2826. const int ref_stride= s->current_picture.linesize[plane_index];
  2827. uint8_t *dst= s->current_picture.data[plane_index];
  2828. uint8_t *src= s-> input_picture.data[plane_index];
  2829. const static DWTELEM zero_dst[4096]; //FIXME
  2830. const int b_stride = s->b_width << s->block_max_depth;
  2831. const int b_height = s->b_height<< s->block_max_depth;
  2832. const int w= p->width;
  2833. const int h= p->height;
  2834. int distortion= 0;
  2835. int rate= 0;
  2836. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2837. for(i=0; i<9; i++){
  2838. int mb_x2= mb_x + (i%3) - 1;
  2839. int mb_y2= mb_y + (i/3) - 1;
  2840. int x= block_w*mb_x2 + block_w/2;
  2841. int y= block_w*mb_y2 + block_w/2;
  2842. add_yblock(s, zero_dst, dst, obmc,
  2843. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  2844. //FIXME find a cleaner/simpler way to skip the outside stuff
  2845. for(y2= y; y2<0; y2++)
  2846. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2847. for(y2= h; y2<y+block_w; y2++)
  2848. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2849. if(x<0){
  2850. for(y2= y; y2<y+block_w; y2++)
  2851. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  2852. }
  2853. if(x+block_w > w){
  2854. for(y2= y; y2<y+block_w; y2++)
  2855. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  2856. }
  2857. assert(block_w== 8 || block_w==16);
  2858. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  2859. }
  2860. if(plane_index==0){
  2861. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  2862. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  2863. /* ..RRRr
  2864. * .RXXx.
  2865. * .RXXx.
  2866. * rxxx.
  2867. */
  2868. if(merged)
  2869. rate = get_block_bits(s, mb_x, mb_y, 2);
  2870. for(i=merged?4:0; i<9; i++){
  2871. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  2872. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  2873. }
  2874. }
  2875. return distortion + rate*penalty_factor;
  2876. }
  2877. 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){
  2878. const int b_stride= s->b_width << s->block_max_depth;
  2879. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2880. BlockNode backup= *block;
  2881. int rd, index, value;
  2882. assert(mb_x>=0 && mb_y>=0);
  2883. assert(mb_x<b_stride);
  2884. if(intra){
  2885. block->color[0] = p[0];
  2886. block->color[1] = p[1];
  2887. block->color[2] = p[2];
  2888. block->type |= BLOCK_INTRA;
  2889. }else{
  2890. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  2891. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  2892. if(s->me_cache[index] == value)
  2893. return 0;
  2894. s->me_cache[index]= value;
  2895. block->mx= p[0];
  2896. block->my= p[1];
  2897. block->type &= ~BLOCK_INTRA;
  2898. }
  2899. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  2900. //FIXME chroma
  2901. if(rd < *best_rd){
  2902. *best_rd= rd;
  2903. return 1;
  2904. }else{
  2905. *block= backup;
  2906. return 0;
  2907. }
  2908. }
  2909. /* special case for int[2] args we discard afterward, fixes compilation prob with gcc 2.95 */
  2910. 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){
  2911. int p[2] = {p0, p1};
  2912. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  2913. }
  2914. static always_inline int check_4block_inter(SnowContext *s, int mb_x, int mb_y, int p0, int p1, int ref, int *best_rd){
  2915. const int b_stride= s->b_width << s->block_max_depth;
  2916. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2917. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  2918. int rd, index, value;
  2919. assert(mb_x>=0 && mb_y>=0);
  2920. assert(mb_x<b_stride);
  2921. assert(((mb_x|mb_y)&1) == 0);
  2922. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  2923. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  2924. if(s->me_cache[index] == value)
  2925. return 0;
  2926. s->me_cache[index]= value;
  2927. block->mx= p0;
  2928. block->my= p1;
  2929. block->ref= ref;
  2930. block->type &= ~BLOCK_INTRA;
  2931. block[1]= block[b_stride]= block[b_stride+1]= *block;
  2932. rd= get_4block_rd(s, mb_x, mb_y, 0);
  2933. //FIXME chroma
  2934. if(rd < *best_rd){
  2935. *best_rd= rd;
  2936. return 1;
  2937. }else{
  2938. block[0]= backup[0];
  2939. block[1]= backup[1];
  2940. block[b_stride]= backup[2];
  2941. block[b_stride+1]= backup[3];
  2942. return 0;
  2943. }
  2944. }
  2945. static void iterative_me(SnowContext *s){
  2946. int pass, mb_x, mb_y;
  2947. const int b_width = s->b_width << s->block_max_depth;
  2948. const int b_height= s->b_height << s->block_max_depth;
  2949. const int b_stride= b_width;
  2950. int color[3];
  2951. {
  2952. RangeCoder r = s->c;
  2953. uint8_t state[sizeof(s->block_state)];
  2954. memcpy(state, s->block_state, sizeof(s->block_state));
  2955. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  2956. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  2957. encode_q_branch(s, 0, mb_x, mb_y);
  2958. s->c = r;
  2959. memcpy(s->block_state, state, sizeof(s->block_state));
  2960. }
  2961. for(pass=0; pass<25; pass++){
  2962. int change= 0;
  2963. for(mb_y= 0; mb_y<b_height; mb_y++){
  2964. for(mb_x= 0; mb_x<b_width; mb_x++){
  2965. int dia_change, i, j, ref;
  2966. int best_rd= INT_MAX, ref_rd;
  2967. BlockNode backup, ref_b;
  2968. const int index= mb_x + mb_y * b_stride;
  2969. BlockNode *block= &s->block[index];
  2970. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  2971. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  2972. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  2973. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  2974. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  2975. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  2976. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  2977. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  2978. const int b_w= (MB_SIZE >> s->block_max_depth);
  2979. uint8_t obmc_edged[b_w*2][b_w*2];
  2980. if(pass && (block->type & BLOCK_OPT))
  2981. continue;
  2982. block->type |= BLOCK_OPT;
  2983. backup= *block;
  2984. if(!s->me_cache_generation)
  2985. memset(s->me_cache, 0, sizeof(s->me_cache));
  2986. s->me_cache_generation += 1<<22;
  2987. //FIXME precalc
  2988. {
  2989. int x, y;
  2990. memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
  2991. if(mb_x==0)
  2992. for(y=0; y<b_w*2; y++)
  2993. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  2994. if(mb_x==b_stride-1)
  2995. for(y=0; y<b_w*2; y++)
  2996. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  2997. if(mb_y==0){
  2998. for(x=0; x<b_w*2; x++)
  2999. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  3000. for(y=1; y<b_w; y++)
  3001. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  3002. }
  3003. if(mb_y==b_height-1){
  3004. for(x=0; x<b_w*2; x++)
  3005. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  3006. for(y=b_w; y<b_w*2-1; y++)
  3007. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  3008. }
  3009. }
  3010. //skip stuff outside the picture
  3011. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1)
  3012. {
  3013. uint8_t *src= s-> input_picture.data[0];
  3014. uint8_t *dst= s->current_picture.data[0];
  3015. const int stride= s->current_picture.linesize[0];
  3016. const int block_w= MB_SIZE >> s->block_max_depth;
  3017. const int sx= block_w*mb_x - block_w/2;
  3018. const int sy= block_w*mb_y - block_w/2;
  3019. const int w= s->plane[0].width;
  3020. const int h= s->plane[0].height;
  3021. int y;
  3022. for(y=sy; y<0; y++)
  3023. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  3024. for(y=h; y<sy+block_w*2; y++)
  3025. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  3026. if(sx<0){
  3027. for(y=sy; y<sy+block_w*2; y++)
  3028. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  3029. }
  3030. if(sx+block_w*2 > w){
  3031. for(y=sy; y<sy+block_w*2; y++)
  3032. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  3033. }
  3034. }
  3035. // intra(black) = neighbors' contribution to the current block
  3036. for(i=0; i<3; i++)
  3037. color[i]= get_dc(s, mb_x, mb_y, i);
  3038. // get previous score (cant be cached due to OBMC)
  3039. if(pass > 0 && (block->type&BLOCK_INTRA)){
  3040. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  3041. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  3042. }else
  3043. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  3044. ref_b= *block;
  3045. ref_rd= best_rd;
  3046. for(ref=0; ref < s->ref_frames; ref++){
  3047. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  3048. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  3049. continue;
  3050. block->ref= ref;
  3051. best_rd= INT_MAX;
  3052. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  3053. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  3054. if(tb)
  3055. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  3056. if(lb)
  3057. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  3058. if(rb)
  3059. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  3060. if(bb)
  3061. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  3062. /* fullpel ME */
  3063. //FIXME avoid subpel interpol / round to nearest integer
  3064. do{
  3065. dia_change=0;
  3066. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  3067. for(j=0; j<i; j++){
  3068. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  3069. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  3070. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  3071. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  3072. }
  3073. }
  3074. }while(dia_change);
  3075. /* subpel ME */
  3076. do{
  3077. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  3078. dia_change=0;
  3079. for(i=0; i<8; i++)
  3080. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  3081. }while(dia_change);
  3082. //FIXME or try the standard 2 pass qpel or similar
  3083. mvr[0][0]= block->mx;
  3084. mvr[0][1]= block->my;
  3085. if(ref_rd > best_rd){
  3086. ref_rd= best_rd;
  3087. ref_b= *block;
  3088. }
  3089. }
  3090. best_rd= ref_rd;
  3091. *block= ref_b;
  3092. #if 1
  3093. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  3094. //FIXME RD style color selection
  3095. #endif
  3096. if(!same_block(block, &backup)){
  3097. if(tb ) tb ->type &= ~BLOCK_OPT;
  3098. if(lb ) lb ->type &= ~BLOCK_OPT;
  3099. if(rb ) rb ->type &= ~BLOCK_OPT;
  3100. if(bb ) bb ->type &= ~BLOCK_OPT;
  3101. if(tlb) tlb->type &= ~BLOCK_OPT;
  3102. if(trb) trb->type &= ~BLOCK_OPT;
  3103. if(blb) blb->type &= ~BLOCK_OPT;
  3104. if(brb) brb->type &= ~BLOCK_OPT;
  3105. change ++;
  3106. }
  3107. }
  3108. }
  3109. av_log(NULL, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  3110. if(!change)
  3111. break;
  3112. }
  3113. if(s->block_max_depth == 1){
  3114. int change= 0;
  3115. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  3116. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  3117. int i;
  3118. int best_rd, init_rd;
  3119. const int index= mb_x + mb_y * b_stride;
  3120. BlockNode *b[4];
  3121. b[0]= &s->block[index];
  3122. b[1]= b[0]+1;
  3123. b[2]= b[0]+b_stride;
  3124. b[3]= b[2]+1;
  3125. if(same_block(b[0], b[1]) &&
  3126. same_block(b[0], b[2]) &&
  3127. same_block(b[0], b[3]))
  3128. continue;
  3129. if(!s->me_cache_generation)
  3130. memset(s->me_cache, 0, sizeof(s->me_cache));
  3131. s->me_cache_generation += 1<<22;
  3132. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  3133. //FIXME more multiref search?
  3134. check_4block_inter(s, mb_x, mb_y,
  3135. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  3136. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  3137. for(i=0; i<4; i++)
  3138. if(!(b[i]->type&BLOCK_INTRA))
  3139. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  3140. if(init_rd != best_rd)
  3141. change++;
  3142. }
  3143. }
  3144. av_log(NULL, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  3145. }
  3146. }
  3147. static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
  3148. const int level= b->level;
  3149. const int w= b->width;
  3150. const int h= b->height;
  3151. const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
  3152. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3153. int x,y, thres1, thres2;
  3154. // START_TIMER
  3155. if(s->qlog == LOSSLESS_QLOG) return;
  3156. bias= bias ? 0 : (3*qmul)>>3;
  3157. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  3158. thres2= 2*thres1;
  3159. if(!bias){
  3160. for(y=0; y<h; y++){
  3161. for(x=0; x<w; x++){
  3162. int i= src[x + y*stride];
  3163. if((unsigned)(i+thres1) > thres2){
  3164. if(i>=0){
  3165. i<<= QEXPSHIFT;
  3166. i/= qmul; //FIXME optimize
  3167. src[x + y*stride]= i;
  3168. }else{
  3169. i= -i;
  3170. i<<= QEXPSHIFT;
  3171. i/= qmul; //FIXME optimize
  3172. src[x + y*stride]= -i;
  3173. }
  3174. }else
  3175. src[x + y*stride]= 0;
  3176. }
  3177. }
  3178. }else{
  3179. for(y=0; y<h; y++){
  3180. for(x=0; x<w; x++){
  3181. int i= src[x + y*stride];
  3182. if((unsigned)(i+thres1) > thres2){
  3183. if(i>=0){
  3184. i<<= QEXPSHIFT;
  3185. i= (i + bias) / qmul; //FIXME optimize
  3186. src[x + y*stride]= i;
  3187. }else{
  3188. i= -i;
  3189. i<<= QEXPSHIFT;
  3190. i= (i + bias) / qmul; //FIXME optimize
  3191. src[x + y*stride]= -i;
  3192. }
  3193. }else
  3194. src[x + y*stride]= 0;
  3195. }
  3196. }
  3197. }
  3198. if(level+1 == s->spatial_decomposition_count){
  3199. // STOP_TIMER("quantize")
  3200. }
  3201. }
  3202. static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride, int start_y, int end_y){
  3203. const int w= b->width;
  3204. const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
  3205. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3206. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  3207. int x,y;
  3208. START_TIMER
  3209. if(s->qlog == LOSSLESS_QLOG) return;
  3210. for(y=start_y; y<end_y; y++){
  3211. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  3212. DWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3213. for(x=0; x<w; x++){
  3214. int i= line[x];
  3215. if(i<0){
  3216. line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  3217. }else if(i>0){
  3218. line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
  3219. }
  3220. }
  3221. }
  3222. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  3223. STOP_TIMER("dquant")
  3224. }
  3225. }
  3226. static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
  3227. const int w= b->width;
  3228. const int h= b->height;
  3229. const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
  3230. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3231. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  3232. int x,y;
  3233. START_TIMER
  3234. if(s->qlog == LOSSLESS_QLOG) return;
  3235. for(y=0; y<h; y++){
  3236. for(x=0; x<w; x++){
  3237. int i= src[x + y*stride];
  3238. if(i<0){
  3239. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  3240. }else if(i>0){
  3241. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  3242. }
  3243. }
  3244. }
  3245. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  3246. STOP_TIMER("dquant")
  3247. }
  3248. }
  3249. static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  3250. const int w= b->width;
  3251. const int h= b->height;
  3252. int x,y;
  3253. for(y=h-1; y>=0; y--){
  3254. for(x=w-1; x>=0; x--){
  3255. int i= x + y*stride;
  3256. if(x){
  3257. if(use_median){
  3258. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3259. else src[i] -= src[i - 1];
  3260. }else{
  3261. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3262. else src[i] -= src[i - 1];
  3263. }
  3264. }else{
  3265. if(y) src[i] -= src[i - stride];
  3266. }
  3267. }
  3268. }
  3269. }
  3270. 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){
  3271. const int w= b->width;
  3272. int x,y;
  3273. // START_TIMER
  3274. DWTELEM * line;
  3275. DWTELEM * prev;
  3276. if (start_y != 0)
  3277. line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3278. for(y=start_y; y<end_y; y++){
  3279. prev = line;
  3280. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  3281. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3282. for(x=0; x<w; x++){
  3283. if(x){
  3284. if(use_median){
  3285. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  3286. else line[x] += line[x - 1];
  3287. }else{
  3288. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  3289. else line[x] += line[x - 1];
  3290. }
  3291. }else{
  3292. if(y) line[x] += prev[x];
  3293. }
  3294. }
  3295. }
  3296. // STOP_TIMER("correlate")
  3297. }
  3298. static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  3299. const int w= b->width;
  3300. const int h= b->height;
  3301. int x,y;
  3302. for(y=0; y<h; y++){
  3303. for(x=0; x<w; x++){
  3304. int i= x + y*stride;
  3305. if(x){
  3306. if(use_median){
  3307. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3308. else src[i] += src[i - 1];
  3309. }else{
  3310. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3311. else src[i] += src[i - 1];
  3312. }
  3313. }else{
  3314. if(y) src[i] += src[i - stride];
  3315. }
  3316. }
  3317. }
  3318. }
  3319. static void encode_header(SnowContext *s){
  3320. int plane_index, level, orientation;
  3321. uint8_t kstate[32];
  3322. memset(kstate, MID_STATE, sizeof(kstate));
  3323. put_rac(&s->c, kstate, s->keyframe);
  3324. if(s->keyframe || s->always_reset)
  3325. reset_contexts(s);
  3326. if(s->keyframe){
  3327. put_symbol(&s->c, s->header_state, s->version, 0);
  3328. put_rac(&s->c, s->header_state, s->always_reset);
  3329. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  3330. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  3331. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3332. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  3333. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  3334. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  3335. put_rac(&s->c, s->header_state, s->spatial_scalability);
  3336. // put_rac(&s->c, s->header_state, s->rate_scalability);
  3337. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  3338. for(plane_index=0; plane_index<2; plane_index++){
  3339. for(level=0; level<s->spatial_decomposition_count; level++){
  3340. for(orientation=level ? 1:0; orientation<4; orientation++){
  3341. if(orientation==2) continue;
  3342. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  3343. }
  3344. }
  3345. }
  3346. }
  3347. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
  3348. put_symbol(&s->c, s->header_state, s->qlog, 1);
  3349. put_symbol(&s->c, s->header_state, s->mv_scale, 0);
  3350. put_symbol(&s->c, s->header_state, s->qbias, 1);
  3351. put_symbol(&s->c, s->header_state, s->block_max_depth, 0);
  3352. }
  3353. static int decode_header(SnowContext *s){
  3354. int plane_index, level, orientation;
  3355. uint8_t kstate[32];
  3356. memset(kstate, MID_STATE, sizeof(kstate));
  3357. s->keyframe= get_rac(&s->c, kstate);
  3358. if(s->keyframe || s->always_reset)
  3359. reset_contexts(s);
  3360. if(s->keyframe){
  3361. s->version= get_symbol(&s->c, s->header_state, 0);
  3362. if(s->version>0){
  3363. av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
  3364. return -1;
  3365. }
  3366. s->always_reset= get_rac(&s->c, s->header_state);
  3367. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  3368. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  3369. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  3370. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  3371. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  3372. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  3373. s->spatial_scalability= get_rac(&s->c, s->header_state);
  3374. // s->rate_scalability= get_rac(&s->c, s->header_state);
  3375. s->max_ref_frames= get_symbol(&s->c, s->header_state, 0)+1;
  3376. for(plane_index=0; plane_index<3; plane_index++){
  3377. for(level=0; level<s->spatial_decomposition_count; level++){
  3378. for(orientation=level ? 1:0; orientation<4; orientation++){
  3379. int q;
  3380. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  3381. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  3382. else q= get_symbol(&s->c, s->header_state, 1);
  3383. s->plane[plane_index].band[level][orientation].qlog= q;
  3384. }
  3385. }
  3386. }
  3387. }
  3388. s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  3389. if(s->spatial_decomposition_type > 2){
  3390. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  3391. return -1;
  3392. }
  3393. s->qlog= get_symbol(&s->c, s->header_state, 1);
  3394. s->mv_scale= get_symbol(&s->c, s->header_state, 0);
  3395. s->qbias= get_symbol(&s->c, s->header_state, 1);
  3396. s->block_max_depth= get_symbol(&s->c, s->header_state, 0);
  3397. if(s->block_max_depth > 1 || s->block_max_depth < 0){
  3398. av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
  3399. s->block_max_depth= 0;
  3400. return -1;
  3401. }
  3402. return 0;
  3403. }
  3404. static void init_qexp(void){
  3405. int i;
  3406. double v=128;
  3407. for(i=0; i<QROOT; i++){
  3408. qexp[i]= lrintf(v);
  3409. v *= pow(2, 1.0 / QROOT);
  3410. }
  3411. }
  3412. static int common_init(AVCodecContext *avctx){
  3413. SnowContext *s = avctx->priv_data;
  3414. int width, height;
  3415. int level, orientation, plane_index, dec;
  3416. int i, j;
  3417. s->avctx= avctx;
  3418. dsputil_init(&s->dsp, avctx);
  3419. #define mcf(dx,dy)\
  3420. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  3421. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  3422. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  3423. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  3424. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  3425. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  3426. mcf( 0, 0)
  3427. mcf( 4, 0)
  3428. mcf( 8, 0)
  3429. mcf(12, 0)
  3430. mcf( 0, 4)
  3431. mcf( 4, 4)
  3432. mcf( 8, 4)
  3433. mcf(12, 4)
  3434. mcf( 0, 8)
  3435. mcf( 4, 8)
  3436. mcf( 8, 8)
  3437. mcf(12, 8)
  3438. mcf( 0,12)
  3439. mcf( 4,12)
  3440. mcf( 8,12)
  3441. mcf(12,12)
  3442. #define mcfh(dx,dy)\
  3443. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  3444. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  3445. mc_block_hpel ## dx ## dy ## 16;\
  3446. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  3447. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  3448. mc_block_hpel ## dx ## dy ## 8;
  3449. mcfh(0, 0)
  3450. mcfh(8, 0)
  3451. mcfh(0, 8)
  3452. mcfh(8, 8)
  3453. if(!qexp[0])
  3454. init_qexp();
  3455. dec= s->spatial_decomposition_count= 5;
  3456. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  3457. s->chroma_h_shift= 1; //FIXME XXX
  3458. s->chroma_v_shift= 1;
  3459. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  3460. width= s->avctx->width;
  3461. height= s->avctx->height;
  3462. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
  3463. s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  3464. s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
  3465. for(plane_index=0; plane_index<3; plane_index++){
  3466. int w= s->avctx->width;
  3467. int h= s->avctx->height;
  3468. if(plane_index){
  3469. w>>= s->chroma_h_shift;
  3470. h>>= s->chroma_v_shift;
  3471. }
  3472. s->plane[plane_index].width = w;
  3473. s->plane[plane_index].height= h;
  3474. //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
  3475. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  3476. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3477. SubBand *b= &s->plane[plane_index].band[level][orientation];
  3478. b->buf= s->spatial_dwt_buffer;
  3479. b->level= level;
  3480. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  3481. b->width = (w + !(orientation&1))>>1;
  3482. b->height= (h + !(orientation>1))>>1;
  3483. b->stride_line = 1 << (s->spatial_decomposition_count - level);
  3484. b->buf_x_offset = 0;
  3485. b->buf_y_offset = 0;
  3486. if(orientation&1){
  3487. b->buf += (w+1)>>1;
  3488. b->buf_x_offset = (w+1)>>1;
  3489. }
  3490. if(orientation>1){
  3491. b->buf += b->stride>>1;
  3492. b->buf_y_offset = b->stride_line >> 1;
  3493. }
  3494. if(level)
  3495. b->parent= &s->plane[plane_index].band[level-1][orientation];
  3496. b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
  3497. }
  3498. w= (w+1)>>1;
  3499. h= (h+1)>>1;
  3500. }
  3501. }
  3502. for(i=0; i<MAX_REF_FRAMES; i++)
  3503. for(j=0; j<MAX_REF_FRAMES; j++)
  3504. scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  3505. reset_contexts(s);
  3506. /*
  3507. width= s->width= avctx->width;
  3508. height= s->height= avctx->height;
  3509. assert(width && height);
  3510. */
  3511. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  3512. return 0;
  3513. }
  3514. static int qscale2qlog(int qscale){
  3515. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  3516. + 61*QROOT/8; //<64 >60
  3517. }
  3518. static void ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  3519. {
  3520. /* estimate the frame's complexity as a sum of weighted dwt coefs.
  3521. * FIXME we know exact mv bits at this point,
  3522. * but ratecontrol isn't set up to include them. */
  3523. uint32_t coef_sum= 0;
  3524. int level, orientation;
  3525. for(level=0; level<s->spatial_decomposition_count; level++){
  3526. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3527. SubBand *b= &s->plane[0].band[level][orientation];
  3528. DWTELEM *buf= b->buf;
  3529. const int w= b->width;
  3530. const int h= b->height;
  3531. const int stride= b->stride;
  3532. const int qlog= clip(2*QROOT + b->qlog, 0, QROOT*16);
  3533. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3534. const int qdiv= (1<<16)/qmul;
  3535. int x, y;
  3536. if(orientation==0)
  3537. decorrelate(s, b, buf, stride, 1, 0);
  3538. for(y=0; y<h; y++)
  3539. for(x=0; x<w; x++)
  3540. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  3541. if(orientation==0)
  3542. correlate(s, b, buf, stride, 1, 0);
  3543. }
  3544. }
  3545. /* ugly, ratecontrol just takes a sqrt again */
  3546. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  3547. assert(coef_sum < INT_MAX);
  3548. if(pict->pict_type == I_TYPE){
  3549. s->m.current_picture.mb_var_sum= coef_sum;
  3550. s->m.current_picture.mc_mb_var_sum= 0;
  3551. }else{
  3552. s->m.current_picture.mc_mb_var_sum= coef_sum;
  3553. s->m.current_picture.mb_var_sum= 0;
  3554. }
  3555. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  3556. s->lambda= pict->quality * 3/2;
  3557. s->qlog= qscale2qlog(pict->quality);
  3558. }
  3559. static void calculate_vissual_weight(SnowContext *s, Plane *p){
  3560. int width = p->width;
  3561. int height= p->height;
  3562. int level, orientation, x, y;
  3563. for(level=0; level<s->spatial_decomposition_count; level++){
  3564. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3565. SubBand *b= &p->band[level][orientation];
  3566. DWTELEM *buf= b->buf;
  3567. int64_t error=0;
  3568. memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
  3569. buf[b->width/2 + b->height/2*b->stride]= 256*256;
  3570. ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3571. for(y=0; y<height; y++){
  3572. for(x=0; x<width; x++){
  3573. int64_t d= s->spatial_dwt_buffer[x + y*width];
  3574. error += d*d;
  3575. }
  3576. }
  3577. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  3578. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
  3579. }
  3580. }
  3581. }
  3582. static int encode_init(AVCodecContext *avctx)
  3583. {
  3584. SnowContext *s = avctx->priv_data;
  3585. int plane_index;
  3586. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  3587. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it may not be decodable with future versions!!!\n"
  3588. "use vstrict=-2 / -strict -2 to use it anyway\n");
  3589. return -1;
  3590. }
  3591. if(avctx->prediction_method == DWT_97
  3592. && (avctx->flags & CODEC_FLAG_QSCALE)
  3593. && avctx->global_quality == 0){
  3594. av_log(avctx, AV_LOG_ERROR, "the 9/7 wavelet is incompatible with lossless mode\n");
  3595. return -1;
  3596. }
  3597. common_init(avctx);
  3598. alloc_blocks(s);
  3599. s->version=0;
  3600. s->m.avctx = avctx;
  3601. s->m.flags = avctx->flags;
  3602. s->m.bit_rate= avctx->bit_rate;
  3603. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  3604. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  3605. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  3606. s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  3607. h263_encode_init(&s->m); //mv_penalty
  3608. s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
  3609. if(avctx->flags&CODEC_FLAG_PASS1){
  3610. if(!avctx->stats_out)
  3611. avctx->stats_out = av_mallocz(256);
  3612. }
  3613. if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  3614. if(ff_rate_control_init(&s->m) < 0)
  3615. return -1;
  3616. }
  3617. s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  3618. for(plane_index=0; plane_index<3; plane_index++){
  3619. calculate_vissual_weight(s, &s->plane[plane_index]);
  3620. }
  3621. avctx->coded_frame= &s->current_picture;
  3622. switch(avctx->pix_fmt){
  3623. // case PIX_FMT_YUV444P:
  3624. // case PIX_FMT_YUV422P:
  3625. case PIX_FMT_YUV420P:
  3626. case PIX_FMT_GRAY8:
  3627. // case PIX_FMT_YUV411P:
  3628. // case PIX_FMT_YUV410P:
  3629. s->colorspace_type= 0;
  3630. break;
  3631. /* case PIX_FMT_RGBA32:
  3632. s->colorspace= 1;
  3633. break;*/
  3634. default:
  3635. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  3636. return -1;
  3637. }
  3638. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  3639. s->chroma_h_shift= 1;
  3640. s->chroma_v_shift= 1;
  3641. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  3642. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  3643. s->avctx->get_buffer(s->avctx, &s->input_picture);
  3644. if(s->avctx->me_method == ME_ITER){
  3645. int i;
  3646. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  3647. for(i=0; i<s->max_ref_frames; i++){
  3648. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  3649. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  3650. }
  3651. }
  3652. return 0;
  3653. }
  3654. static int frame_start(SnowContext *s){
  3655. AVFrame tmp;
  3656. int w= s->avctx->width; //FIXME round up to x16 ?
  3657. int h= s->avctx->height;
  3658. if(s->current_picture.data[0]){
  3659. draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  3660. draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  3661. draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  3662. }
  3663. tmp= s->last_picture[s->max_ref_frames-1];
  3664. memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
  3665. s->last_picture[0]= s->current_picture;
  3666. s->current_picture= tmp;
  3667. if(s->keyframe){
  3668. s->ref_frames= 0;
  3669. }else{
  3670. int i;
  3671. for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
  3672. if(i && s->last_picture[i-1].key_frame)
  3673. break;
  3674. s->ref_frames= i;
  3675. }
  3676. s->current_picture.reference= 1;
  3677. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  3678. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  3679. return -1;
  3680. }
  3681. s->current_picture.key_frame= s->keyframe;
  3682. return 0;
  3683. }
  3684. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  3685. SnowContext *s = avctx->priv_data;
  3686. RangeCoder * const c= &s->c;
  3687. AVFrame *pict = data;
  3688. const int width= s->avctx->width;
  3689. const int height= s->avctx->height;
  3690. int level, orientation, plane_index, i, y;
  3691. ff_init_range_encoder(c, buf, buf_size);
  3692. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3693. for(i=0; i<3; i++){
  3694. int shift= !!i;
  3695. for(y=0; y<(height>>shift); y++)
  3696. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  3697. &pict->data[i][y * pict->linesize[i]],
  3698. width>>shift);
  3699. }
  3700. s->new_picture = *pict;
  3701. s->m.picture_number= avctx->frame_number;
  3702. if(avctx->flags&CODEC_FLAG_PASS2){
  3703. s->m.pict_type =
  3704. pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  3705. s->keyframe= pict->pict_type==FF_I_TYPE;
  3706. if(!(avctx->flags&CODEC_FLAG_QSCALE))
  3707. pict->quality= ff_rate_estimate_qscale(&s->m, 0);
  3708. }else{
  3709. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  3710. s->m.pict_type=
  3711. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  3712. }
  3713. if(s->pass1_rc && avctx->frame_number == 0)
  3714. pict->quality= 2*FF_QP2LAMBDA;
  3715. if(pict->quality){
  3716. s->qlog= qscale2qlog(pict->quality);
  3717. s->lambda = pict->quality * 3/2;
  3718. }
  3719. if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
  3720. s->qlog= LOSSLESS_QLOG;
  3721. s->lambda = 0;
  3722. }//else keep previous frame's qlog until after motion est
  3723. frame_start(s);
  3724. s->m.current_picture_ptr= &s->m.current_picture;
  3725. if(pict->pict_type == P_TYPE){
  3726. int block_width = (width +15)>>4;
  3727. int block_height= (height+15)>>4;
  3728. int stride= s->current_picture.linesize[0];
  3729. assert(s->current_picture.data[0]);
  3730. assert(s->last_picture[0].data[0]);
  3731. s->m.avctx= s->avctx;
  3732. s->m.current_picture.data[0]= s->current_picture.data[0];
  3733. s->m. last_picture.data[0]= s->last_picture[0].data[0];
  3734. s->m. new_picture.data[0]= s-> input_picture.data[0];
  3735. s->m. last_picture_ptr= &s->m. last_picture;
  3736. s->m.linesize=
  3737. s->m. last_picture.linesize[0]=
  3738. s->m. new_picture.linesize[0]=
  3739. s->m.current_picture.linesize[0]= stride;
  3740. s->m.uvlinesize= s->current_picture.linesize[1];
  3741. s->m.width = width;
  3742. s->m.height= height;
  3743. s->m.mb_width = block_width;
  3744. s->m.mb_height= block_height;
  3745. s->m.mb_stride= s->m.mb_width+1;
  3746. s->m.b8_stride= 2*s->m.mb_width+1;
  3747. s->m.f_code=1;
  3748. s->m.pict_type= pict->pict_type;
  3749. s->m.me_method= s->avctx->me_method;
  3750. s->m.me.scene_change_score=0;
  3751. s->m.flags= s->avctx->flags;
  3752. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  3753. s->m.out_format= FMT_H263;
  3754. s->m.unrestricted_mv= 1;
  3755. s->m.lambda = s->lambda;
  3756. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  3757. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  3758. s->m.dsp= s->dsp; //move
  3759. ff_init_me(&s->m);
  3760. s->dsp= s->m.dsp;
  3761. }
  3762. redo_frame:
  3763. s->m.pict_type = pict->pict_type;
  3764. s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
  3765. encode_header(s);
  3766. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3767. encode_blocks(s);
  3768. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  3769. for(plane_index=0; plane_index<3; plane_index++){
  3770. Plane *p= &s->plane[plane_index];
  3771. int w= p->width;
  3772. int h= p->height;
  3773. int x, y;
  3774. // int bits= put_bits_count(&s->c.pb);
  3775. if(!(avctx->flags2 & CODEC_FLAG2_MEMC_ONLY)){
  3776. //FIXME optimize
  3777. if(pict->data[plane_index]) //FIXME gray hack
  3778. for(y=0; y<h; y++){
  3779. for(x=0; x<w; x++){
  3780. s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3781. }
  3782. }
  3783. predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
  3784. if( plane_index==0
  3785. && pict->pict_type == P_TYPE
  3786. && !(avctx->flags&CODEC_FLAG_PASS2)
  3787. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3788. ff_init_range_encoder(c, buf, buf_size);
  3789. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3790. pict->pict_type= FF_I_TYPE;
  3791. s->keyframe=1;
  3792. s->current_picture.key_frame=1;
  3793. reset_contexts(s);
  3794. goto redo_frame;
  3795. }
  3796. if(s->qlog == LOSSLESS_QLOG){
  3797. for(y=0; y<h; y++){
  3798. for(x=0; x<w; x++){
  3799. s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  3800. }
  3801. }
  3802. }
  3803. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3804. if(s->pass1_rc && plane_index==0)
  3805. ratecontrol_1pass(s, pict);
  3806. for(level=0; level<s->spatial_decomposition_count; level++){
  3807. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3808. SubBand *b= &p->band[level][orientation];
  3809. quantize(s, b, b->buf, b->stride, s->qbias);
  3810. if(orientation==0)
  3811. decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
  3812. encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  3813. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3814. if(orientation==0)
  3815. correlate(s, b, b->buf, b->stride, 1, 0);
  3816. }
  3817. }
  3818. // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
  3819. for(level=0; level<s->spatial_decomposition_count; level++){
  3820. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3821. SubBand *b= &p->band[level][orientation];
  3822. dequantize(s, b, b->buf, b->stride);
  3823. }
  3824. }
  3825. ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3826. if(s->qlog == LOSSLESS_QLOG){
  3827. for(y=0; y<h; y++){
  3828. for(x=0; x<w; x++){
  3829. s->spatial_dwt_buffer[y*w + x]<<=FRAC_BITS;
  3830. }
  3831. }
  3832. }
  3833. {START_TIMER
  3834. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3835. STOP_TIMER("pred-conv")}
  3836. }else{
  3837. //ME/MC only
  3838. if(pict->pict_type == I_TYPE){
  3839. for(y=0; y<h; y++){
  3840. for(x=0; x<w; x++){
  3841. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  3842. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3843. }
  3844. }
  3845. }else{
  3846. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  3847. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3848. }
  3849. }
  3850. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3851. int64_t error= 0;
  3852. if(pict->data[plane_index]) //FIXME gray hack
  3853. for(y=0; y<h; y++){
  3854. for(x=0; x<w; x++){
  3855. 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];
  3856. error += d*d;
  3857. }
  3858. }
  3859. s->avctx->error[plane_index] += error;
  3860. s->current_picture.error[plane_index] = error;
  3861. }
  3862. }
  3863. if(s->last_picture[s->max_ref_frames-1].data[0])
  3864. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  3865. s->current_picture.coded_picture_number = avctx->frame_number;
  3866. s->current_picture.pict_type = pict->pict_type;
  3867. s->current_picture.quality = pict->quality;
  3868. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3869. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  3870. s->m.current_picture.display_picture_number =
  3871. s->m.current_picture.coded_picture_number = avctx->frame_number;
  3872. s->m.current_picture.quality = pict->quality;
  3873. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  3874. if(s->pass1_rc)
  3875. ff_rate_estimate_qscale(&s->m, 0);
  3876. if(avctx->flags&CODEC_FLAG_PASS1)
  3877. ff_write_pass1_stats(&s->m);
  3878. s->m.last_pict_type = s->m.pict_type;
  3879. emms_c();
  3880. return ff_rac_terminate(c);
  3881. }
  3882. static void common_end(SnowContext *s){
  3883. int plane_index, level, orientation, i;
  3884. av_freep(&s->spatial_dwt_buffer);
  3885. av_freep(&s->m.me.scratchpad);
  3886. av_freep(&s->m.me.map);
  3887. av_freep(&s->m.me.score_map);
  3888. av_freep(&s->m.obmc_scratchpad);
  3889. av_freep(&s->block);
  3890. for(i=0; i<MAX_REF_FRAMES; i++){
  3891. av_freep(&s->ref_mvs[i]);
  3892. av_freep(&s->ref_scores[i]);
  3893. if(s->last_picture[i].data[0])
  3894. s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
  3895. }
  3896. for(plane_index=0; plane_index<3; plane_index++){
  3897. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  3898. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3899. SubBand *b= &s->plane[plane_index].band[level][orientation];
  3900. av_freep(&b->x_coeff);
  3901. }
  3902. }
  3903. }
  3904. }
  3905. static int encode_end(AVCodecContext *avctx)
  3906. {
  3907. SnowContext *s = avctx->priv_data;
  3908. common_end(s);
  3909. av_free(avctx->stats_out);
  3910. return 0;
  3911. }
  3912. static int decode_init(AVCodecContext *avctx)
  3913. {
  3914. SnowContext *s = avctx->priv_data;
  3915. int block_size;
  3916. avctx->pix_fmt= PIX_FMT_YUV420P;
  3917. common_init(avctx);
  3918. block_size = MB_SIZE >> s->block_max_depth;
  3919. 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);
  3920. return 0;
  3921. }
  3922. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  3923. SnowContext *s = avctx->priv_data;
  3924. RangeCoder * const c= &s->c;
  3925. int bytes_read;
  3926. AVFrame *picture = data;
  3927. int level, orientation, plane_index;
  3928. ff_init_range_decoder(c, buf, buf_size);
  3929. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3930. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  3931. decode_header(s);
  3932. if(!s->block) alloc_blocks(s);
  3933. frame_start(s);
  3934. //keyframe flag dupliaction mess FIXME
  3935. if(avctx->debug&FF_DEBUG_PICT_INFO)
  3936. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  3937. decode_blocks(s);
  3938. for(plane_index=0; plane_index<3; plane_index++){
  3939. Plane *p= &s->plane[plane_index];
  3940. int w= p->width;
  3941. int h= p->height;
  3942. int x, y;
  3943. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  3944. if(s->avctx->debug&2048){
  3945. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  3946. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3947. for(y=0; y<h; y++){
  3948. for(x=0; x<w; x++){
  3949. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  3950. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  3951. }
  3952. }
  3953. }
  3954. { START_TIMER
  3955. for(level=0; level<s->spatial_decomposition_count; level++){
  3956. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3957. SubBand *b= &p->band[level][orientation];
  3958. unpack_coeffs(s, b, b->parent, orientation);
  3959. }
  3960. }
  3961. STOP_TIMER("unpack coeffs");
  3962. }
  3963. {START_TIMER
  3964. const int mb_h= s->b_height << s->block_max_depth;
  3965. const int block_size = MB_SIZE >> s->block_max_depth;
  3966. const int block_w = plane_index ? block_size/2 : block_size;
  3967. int mb_y;
  3968. dwt_compose_t cs[MAX_DECOMPOSITIONS];
  3969. int yd=0, yq=0;
  3970. int y;
  3971. int end_y;
  3972. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3973. for(mb_y=0; mb_y<=mb_h; mb_y++){
  3974. int slice_starty = block_w*mb_y;
  3975. int slice_h = block_w*(mb_y+1);
  3976. if (!(s->keyframe || s->avctx->debug&512)){
  3977. slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
  3978. slice_h -= (block_w >> 1);
  3979. }
  3980. {
  3981. START_TIMER
  3982. for(level=0; level<s->spatial_decomposition_count; level++){
  3983. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3984. SubBand *b= &p->band[level][orientation];
  3985. int start_y;
  3986. int end_y;
  3987. int our_mb_start = mb_y;
  3988. int our_mb_end = (mb_y + 1);
  3989. const int extra= 3;
  3990. start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  3991. end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  3992. if (!(s->keyframe || s->avctx->debug&512)){
  3993. start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  3994. end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  3995. }
  3996. start_y = FFMIN(b->height, start_y);
  3997. end_y = FFMIN(b->height, end_y);
  3998. if (start_y != end_y){
  3999. if (orientation == 0){
  4000. SubBand * correlate_band = &p->band[0][0];
  4001. int correlate_end_y = FFMIN(b->height, end_y + 1);
  4002. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  4003. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  4004. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  4005. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, start_y, end_y);
  4006. }
  4007. else
  4008. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  4009. }
  4010. }
  4011. }
  4012. STOP_TIMER("decode_subband_slice");
  4013. }
  4014. { START_TIMER
  4015. for(; yd<slice_h; yd+=4){
  4016. ff_spatial_idwt_buffered_slice(&s->dsp, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  4017. }
  4018. STOP_TIMER("idwt slice");}
  4019. if(s->qlog == LOSSLESS_QLOG){
  4020. for(; yq<slice_h && yq<h; yq++){
  4021. DWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  4022. for(x=0; x<w; x++){
  4023. line[x] <<= FRAC_BITS;
  4024. }
  4025. }
  4026. }
  4027. predict_slice_buffered(s, &s->sb, s->spatial_dwt_buffer, plane_index, 1, mb_y);
  4028. y = FFMIN(p->height, slice_starty);
  4029. end_y = FFMIN(p->height, slice_h);
  4030. while(y < end_y)
  4031. slice_buffer_release(&s->sb, y++);
  4032. }
  4033. slice_buffer_flush(&s->sb);
  4034. STOP_TIMER("idwt + predict_slices")}
  4035. }
  4036. emms_c();
  4037. if(s->last_picture[s->max_ref_frames-1].data[0])
  4038. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  4039. if(!(s->avctx->debug&2048))
  4040. *picture= s->current_picture;
  4041. else
  4042. *picture= s->mconly_picture;
  4043. *data_size = sizeof(AVFrame);
  4044. bytes_read= c->bytestream - c->bytestream_start;
  4045. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  4046. return bytes_read;
  4047. }
  4048. static int decode_end(AVCodecContext *avctx)
  4049. {
  4050. SnowContext *s = avctx->priv_data;
  4051. slice_buffer_destroy(&s->sb);
  4052. common_end(s);
  4053. return 0;
  4054. }
  4055. AVCodec snow_decoder = {
  4056. "snow",
  4057. CODEC_TYPE_VIDEO,
  4058. CODEC_ID_SNOW,
  4059. sizeof(SnowContext),
  4060. decode_init,
  4061. NULL,
  4062. decode_end,
  4063. decode_frame,
  4064. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  4065. NULL
  4066. };
  4067. #ifdef CONFIG_ENCODERS
  4068. AVCodec snow_encoder = {
  4069. "snow",
  4070. CODEC_TYPE_VIDEO,
  4071. CODEC_ID_SNOW,
  4072. sizeof(SnowContext),
  4073. encode_init,
  4074. encode_frame,
  4075. encode_end,
  4076. };
  4077. #endif
  4078. #if 0
  4079. #undef malloc
  4080. #undef free
  4081. #undef printf
  4082. int main(){
  4083. int width=256;
  4084. int height=256;
  4085. int buffer[2][width*height];
  4086. SnowContext s;
  4087. int i;
  4088. s.spatial_decomposition_count=6;
  4089. s.spatial_decomposition_type=1;
  4090. printf("testing 5/3 DWT\n");
  4091. for(i=0; i<width*height; i++)
  4092. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  4093. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4094. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4095. for(i=0; i<width*height; i++)
  4096. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  4097. printf("testing 9/7 DWT\n");
  4098. s.spatial_decomposition_type=0;
  4099. for(i=0; i<width*height; i++)
  4100. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  4101. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4102. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4103. for(i=0; i<width*height; i++)
  4104. if(ABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  4105. #if 0
  4106. printf("testing AC coder\n");
  4107. memset(s.header_state, 0, sizeof(s.header_state));
  4108. ff_init_range_encoder(&s.c, buffer[0], 256*256);
  4109. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  4110. for(i=-256; i<256; i++){
  4111. START_TIMER
  4112. put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
  4113. STOP_TIMER("put_symbol")
  4114. }
  4115. ff_rac_terminate(&s.c);
  4116. memset(s.header_state, 0, sizeof(s.header_state));
  4117. ff_init_range_decoder(&s.c, buffer[0], 256*256);
  4118. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  4119. for(i=-256; i<256; i++){
  4120. int j;
  4121. START_TIMER
  4122. j= get_symbol(&s.c, s.header_state, 1);
  4123. STOP_TIMER("get_symbol")
  4124. if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
  4125. }
  4126. #endif
  4127. {
  4128. int level, orientation, x, y;
  4129. int64_t errors[8][4];
  4130. int64_t g=0;
  4131. memset(errors, 0, sizeof(errors));
  4132. s.spatial_decomposition_count=3;
  4133. s.spatial_decomposition_type=0;
  4134. for(level=0; level<s.spatial_decomposition_count; level++){
  4135. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  4136. int w= width >> (s.spatial_decomposition_count-level);
  4137. int h= height >> (s.spatial_decomposition_count-level);
  4138. int stride= width << (s.spatial_decomposition_count-level);
  4139. DWTELEM *buf= buffer[0];
  4140. int64_t error=0;
  4141. if(orientation&1) buf+=w;
  4142. if(orientation>1) buf+=stride>>1;
  4143. memset(buffer[0], 0, sizeof(int)*width*height);
  4144. buf[w/2 + h/2*stride]= 256*256;
  4145. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4146. for(y=0; y<height; y++){
  4147. for(x=0; x<width; x++){
  4148. int64_t d= buffer[0][x + y*width];
  4149. error += d*d;
  4150. if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
  4151. }
  4152. if(ABS(height/2-y)<9 && level==2) printf("\n");
  4153. }
  4154. error= (int)(sqrt(error)+0.5);
  4155. errors[level][orientation]= error;
  4156. if(g) g=ff_gcd(g, error);
  4157. else g= error;
  4158. }
  4159. }
  4160. printf("static int const visual_weight[][4]={\n");
  4161. for(level=0; level<s.spatial_decomposition_count; level++){
  4162. printf(" {");
  4163. for(orientation=0; orientation<4; orientation++){
  4164. printf("%8lld,", errors[level][orientation]/g);
  4165. }
  4166. printf("},\n");
  4167. }
  4168. printf("};\n");
  4169. {
  4170. int level=2;
  4171. int orientation=3;
  4172. int w= width >> (s.spatial_decomposition_count-level);
  4173. int h= height >> (s.spatial_decomposition_count-level);
  4174. int stride= width << (s.spatial_decomposition_count-level);
  4175. DWTELEM *buf= buffer[0];
  4176. int64_t error=0;
  4177. buf+=w;
  4178. buf+=stride>>1;
  4179. memset(buffer[0], 0, sizeof(int)*width*height);
  4180. #if 1
  4181. for(y=0; y<height; y++){
  4182. for(x=0; x<width; x++){
  4183. int tab[4]={0,2,3,1};
  4184. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  4185. }
  4186. }
  4187. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4188. #else
  4189. for(y=0; y<h; y++){
  4190. for(x=0; x<w; x++){
  4191. buf[x + y*stride ]=169;
  4192. buf[x + y*stride-w]=64;
  4193. }
  4194. }
  4195. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4196. #endif
  4197. for(y=0; y<height; y++){
  4198. for(x=0; x<width; x++){
  4199. int64_t d= buffer[0][x + y*width];
  4200. error += d*d;
  4201. if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
  4202. }
  4203. if(ABS(height/2-y)<9) printf("\n");
  4204. }
  4205. }
  4206. }
  4207. return 0;
  4208. }
  4209. #endif