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.

4766 lines
172KB

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