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.

4154 lines
159KB

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