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.

4715 lines
167KB

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