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.

4690 lines
169KB

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