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.

4150 lines
159KB

  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "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. ff_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], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  1783. s->dsp.draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  1784. s->dsp.draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  1785. }
  1786. release_buffer(s->avctx);
  1787. tmp= s->last_picture[s->max_ref_frames-1];
  1788. memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
  1789. memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
  1790. if(USE_HALFPEL_PLANE && s->current_picture.data[0])
  1791. halfpel_interpol(s, s->halfpel_plane[0], &s->current_picture);
  1792. s->last_picture[0]= s->current_picture;
  1793. s->current_picture= tmp;
  1794. if(s->keyframe){
  1795. s->ref_frames= 0;
  1796. }else{
  1797. int i;
  1798. for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
  1799. if(i && s->last_picture[i-1].key_frame)
  1800. break;
  1801. s->ref_frames= i;
  1802. if(s->ref_frames==0){
  1803. av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
  1804. return -1;
  1805. }
  1806. }
  1807. s->current_picture.reference= 1;
  1808. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  1809. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1810. return -1;
  1811. }
  1812. s->current_picture.key_frame= s->keyframe;
  1813. return 0;
  1814. }
  1815. static av_cold void common_end(SnowContext *s){
  1816. int plane_index, level, orientation, i;
  1817. av_freep(&s->spatial_dwt_buffer);
  1818. av_freep(&s->spatial_idwt_buffer);
  1819. s->m.me.temp= NULL;
  1820. av_freep(&s->m.me.scratchpad);
  1821. av_freep(&s->m.me.map);
  1822. av_freep(&s->m.me.score_map);
  1823. av_freep(&s->m.obmc_scratchpad);
  1824. av_freep(&s->block);
  1825. av_freep(&s->scratchbuf);
  1826. for(i=0; i<MAX_REF_FRAMES; i++){
  1827. av_freep(&s->ref_mvs[i]);
  1828. av_freep(&s->ref_scores[i]);
  1829. if(s->last_picture[i].data[0])
  1830. s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
  1831. }
  1832. for(plane_index=0; plane_index<3; plane_index++){
  1833. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  1834. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1835. SubBand *b= &s->plane[plane_index].band[level][orientation];
  1836. av_freep(&b->x_coeff);
  1837. }
  1838. }
  1839. }
  1840. if (s->mconly_picture.data[0])
  1841. s->avctx->release_buffer(s->avctx, &s->mconly_picture);
  1842. if (s->current_picture.data[0])
  1843. s->avctx->release_buffer(s->avctx, &s->current_picture);
  1844. }
  1845. static av_cold int decode_init(AVCodecContext *avctx)
  1846. {
  1847. avctx->pix_fmt= PIX_FMT_YUV420P;
  1848. common_init(avctx);
  1849. return 0;
  1850. }
  1851. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1852. const uint8_t *buf = avpkt->data;
  1853. int buf_size = avpkt->size;
  1854. SnowContext *s = avctx->priv_data;
  1855. RangeCoder * const c= &s->c;
  1856. int bytes_read;
  1857. AVFrame *picture = data;
  1858. int level, orientation, plane_index;
  1859. ff_init_range_decoder(c, buf, buf_size);
  1860. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1861. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  1862. if(decode_header(s)<0)
  1863. return -1;
  1864. common_init_after_header(avctx);
  1865. // realloc slice buffer for the case that spatial_decomposition_count changed
  1866. ff_slice_buffer_destroy(&s->sb);
  1867. 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);
  1868. for(plane_index=0; plane_index<3; plane_index++){
  1869. Plane *p= &s->plane[plane_index];
  1870. p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
  1871. && p->hcoeff[1]==-10
  1872. && p->hcoeff[2]==2;
  1873. }
  1874. alloc_blocks(s);
  1875. if(frame_start(s) < 0)
  1876. return -1;
  1877. //keyframe flag duplication mess FIXME
  1878. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1879. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  1880. decode_blocks(s);
  1881. for(plane_index=0; plane_index<3; plane_index++){
  1882. Plane *p= &s->plane[plane_index];
  1883. int w= p->width;
  1884. int h= p->height;
  1885. int x, y;
  1886. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  1887. if(s->avctx->debug&2048){
  1888. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  1889. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  1890. for(y=0; y<h; y++){
  1891. for(x=0; x<w; x++){
  1892. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  1893. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  1894. }
  1895. }
  1896. }
  1897. {
  1898. for(level=0; level<s->spatial_decomposition_count; level++){
  1899. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1900. SubBand *b= &p->band[level][orientation];
  1901. unpack_coeffs(s, b, b->parent, orientation);
  1902. }
  1903. }
  1904. }
  1905. {
  1906. const int mb_h= s->b_height << s->block_max_depth;
  1907. const int block_size = MB_SIZE >> s->block_max_depth;
  1908. const int block_w = plane_index ? block_size/2 : block_size;
  1909. int mb_y;
  1910. DWTCompose cs[MAX_DECOMPOSITIONS];
  1911. int yd=0, yq=0;
  1912. int y;
  1913. int end_y;
  1914. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1915. for(mb_y=0; mb_y<=mb_h; mb_y++){
  1916. int slice_starty = block_w*mb_y;
  1917. int slice_h = block_w*(mb_y+1);
  1918. if (!(s->keyframe || s->avctx->debug&512)){
  1919. slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
  1920. slice_h -= (block_w >> 1);
  1921. }
  1922. for(level=0; level<s->spatial_decomposition_count; level++){
  1923. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1924. SubBand *b= &p->band[level][orientation];
  1925. int start_y;
  1926. int end_y;
  1927. int our_mb_start = mb_y;
  1928. int our_mb_end = (mb_y + 1);
  1929. const int extra= 3;
  1930. start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  1931. end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  1932. if (!(s->keyframe || s->avctx->debug&512)){
  1933. start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  1934. end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  1935. }
  1936. start_y = FFMIN(b->height, start_y);
  1937. end_y = FFMIN(b->height, end_y);
  1938. if (start_y != end_y){
  1939. if (orientation == 0){
  1940. SubBand * correlate_band = &p->band[0][0];
  1941. int correlate_end_y = FFMIN(b->height, end_y + 1);
  1942. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  1943. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  1944. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  1945. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
  1946. }
  1947. else
  1948. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  1949. }
  1950. }
  1951. }
  1952. for(; yd<slice_h; yd+=4){
  1953. ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  1954. }
  1955. if(s->qlog == LOSSLESS_QLOG){
  1956. for(; yq<slice_h && yq<h; yq++){
  1957. IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  1958. for(x=0; x<w; x++){
  1959. line[x] <<= FRAC_BITS;
  1960. }
  1961. }
  1962. }
  1963. predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
  1964. y = FFMIN(p->height, slice_starty);
  1965. end_y = FFMIN(p->height, slice_h);
  1966. while(y < end_y)
  1967. ff_slice_buffer_release(&s->sb, y++);
  1968. }
  1969. ff_slice_buffer_flush(&s->sb);
  1970. }
  1971. }
  1972. emms_c();
  1973. release_buffer(avctx);
  1974. if(!(s->avctx->debug&2048))
  1975. *picture= s->current_picture;
  1976. else
  1977. *picture= s->mconly_picture;
  1978. *data_size = sizeof(AVFrame);
  1979. bytes_read= c->bytestream - c->bytestream_start;
  1980. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  1981. return bytes_read;
  1982. }
  1983. static av_cold int decode_end(AVCodecContext *avctx)
  1984. {
  1985. SnowContext *s = avctx->priv_data;
  1986. ff_slice_buffer_destroy(&s->sb);
  1987. common_end(s);
  1988. return 0;
  1989. }
  1990. AVCodec snow_decoder = {
  1991. "snow",
  1992. AVMEDIA_TYPE_VIDEO,
  1993. CODEC_ID_SNOW,
  1994. sizeof(SnowContext),
  1995. decode_init,
  1996. NULL,
  1997. decode_end,
  1998. decode_frame,
  1999. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  2000. NULL,
  2001. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  2002. };
  2003. #if CONFIG_SNOW_ENCODER
  2004. static av_cold int encode_init(AVCodecContext *avctx)
  2005. {
  2006. SnowContext *s = avctx->priv_data;
  2007. int plane_index;
  2008. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  2009. av_log(avctx, AV_LOG_ERROR, "This codec is under development, files encoded with it may not be decodable with future versions!!!\n"
  2010. "Use vstrict=-2 / -strict -2 to use it anyway.\n");
  2011. return -1;
  2012. }
  2013. if(avctx->prediction_method == DWT_97
  2014. && (avctx->flags & CODEC_FLAG_QSCALE)
  2015. && avctx->global_quality == 0){
  2016. av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
  2017. return -1;
  2018. }
  2019. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  2020. s->mv_scale = (avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  2021. s->block_max_depth= (avctx->flags & CODEC_FLAG_4MV ) ? 1 : 0;
  2022. for(plane_index=0; plane_index<3; plane_index++){
  2023. s->plane[plane_index].diag_mc= 1;
  2024. s->plane[plane_index].htaps= 6;
  2025. s->plane[plane_index].hcoeff[0]= 40;
  2026. s->plane[plane_index].hcoeff[1]= -10;
  2027. s->plane[plane_index].hcoeff[2]= 2;
  2028. s->plane[plane_index].fast_mc= 1;
  2029. }
  2030. common_init(avctx);
  2031. alloc_blocks(s);
  2032. s->version=0;
  2033. s->m.avctx = avctx;
  2034. s->m.flags = avctx->flags;
  2035. s->m.bit_rate= avctx->bit_rate;
  2036. s->m.me.temp =
  2037. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  2038. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2039. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2040. s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  2041. h263_encode_init(&s->m); //mv_penalty
  2042. s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
  2043. if(avctx->flags&CODEC_FLAG_PASS1){
  2044. if(!avctx->stats_out)
  2045. avctx->stats_out = av_mallocz(256);
  2046. }
  2047. if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  2048. if(ff_rate_control_init(&s->m) < 0)
  2049. return -1;
  2050. }
  2051. s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  2052. avctx->coded_frame= &s->current_picture;
  2053. switch(avctx->pix_fmt){
  2054. // case PIX_FMT_YUV444P:
  2055. // case PIX_FMT_YUV422P:
  2056. case PIX_FMT_YUV420P:
  2057. case PIX_FMT_GRAY8:
  2058. // case PIX_FMT_YUV411P:
  2059. // case PIX_FMT_YUV410P:
  2060. s->colorspace_type= 0;
  2061. break;
  2062. /* case PIX_FMT_RGB32:
  2063. s->colorspace= 1;
  2064. break;*/
  2065. default:
  2066. av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
  2067. return -1;
  2068. }
  2069. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  2070. s->chroma_h_shift= 1;
  2071. s->chroma_v_shift= 1;
  2072. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  2073. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  2074. s->avctx->get_buffer(s->avctx, &s->input_picture);
  2075. if(s->avctx->me_method == ME_ITER){
  2076. int i;
  2077. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  2078. for(i=0; i<s->max_ref_frames; i++){
  2079. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  2080. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  2081. }
  2082. }
  2083. return 0;
  2084. }
  2085. //near copy & paste from dsputil, FIXME
  2086. static int pix_sum(uint8_t * pix, int line_size, int w)
  2087. {
  2088. int s, i, j;
  2089. s = 0;
  2090. for (i = 0; i < w; i++) {
  2091. for (j = 0; j < w; j++) {
  2092. s += pix[0];
  2093. pix ++;
  2094. }
  2095. pix += line_size - w;
  2096. }
  2097. return s;
  2098. }
  2099. //near copy & paste from dsputil, FIXME
  2100. static int pix_norm1(uint8_t * pix, int line_size, int w)
  2101. {
  2102. int s, i, j;
  2103. uint32_t *sq = ff_squareTbl + 256;
  2104. s = 0;
  2105. for (i = 0; i < w; i++) {
  2106. for (j = 0; j < w; j ++) {
  2107. s += sq[pix[0]];
  2108. pix ++;
  2109. }
  2110. pix += line_size - w;
  2111. }
  2112. return s;
  2113. }
  2114. //FIXME copy&paste
  2115. #define P_LEFT P[1]
  2116. #define P_TOP P[2]
  2117. #define P_TOPRIGHT P[3]
  2118. #define P_MEDIAN P[4]
  2119. #define P_MV1 P[9]
  2120. #define FLAG_QPEL 1 //must be 1
  2121. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  2122. uint8_t p_buffer[1024];
  2123. uint8_t i_buffer[1024];
  2124. uint8_t p_state[sizeof(s->block_state)];
  2125. uint8_t i_state[sizeof(s->block_state)];
  2126. RangeCoder pc, ic;
  2127. uint8_t *pbbak= s->c.bytestream;
  2128. uint8_t *pbbak_start= s->c.bytestream_start;
  2129. int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
  2130. const int w= s->b_width << s->block_max_depth;
  2131. const int h= s->b_height << s->block_max_depth;
  2132. const int rem_depth= s->block_max_depth - level;
  2133. const int index= (x + y*w) << rem_depth;
  2134. const int block_w= 1<<(LOG2_MB_SIZE - level);
  2135. int trx= (x+1)<<rem_depth;
  2136. int try= (y+1)<<rem_depth;
  2137. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2138. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  2139. const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  2140. const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  2141. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  2142. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  2143. int pl = left->color[0];
  2144. int pcb= left->color[1];
  2145. int pcr= left->color[2];
  2146. int pmx, pmy;
  2147. int mx=0, my=0;
  2148. int l,cr,cb;
  2149. const int stride= s->current_picture.linesize[0];
  2150. const int uvstride= s->current_picture.linesize[1];
  2151. uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y* stride)*block_w,
  2152. s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
  2153. s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
  2154. int P[10][2];
  2155. int16_t last_mv[3][2];
  2156. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  2157. const int shift= 1+qpel;
  2158. MotionEstContext *c= &s->m.me;
  2159. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  2160. int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2161. int my_context= av_log2(2*FFABS(left->my - top->my));
  2162. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  2163. int ref, best_ref, ref_score, ref_mx, ref_my;
  2164. assert(sizeof(s->block_state) >= 256);
  2165. if(s->keyframe){
  2166. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  2167. return 0;
  2168. }
  2169. // clip predictors / edge ?
  2170. P_LEFT[0]= left->mx;
  2171. P_LEFT[1]= left->my;
  2172. P_TOP [0]= top->mx;
  2173. P_TOP [1]= top->my;
  2174. P_TOPRIGHT[0]= tr->mx;
  2175. P_TOPRIGHT[1]= tr->my;
  2176. last_mv[0][0]= s->block[index].mx;
  2177. last_mv[0][1]= s->block[index].my;
  2178. last_mv[1][0]= right->mx;
  2179. last_mv[1][1]= right->my;
  2180. last_mv[2][0]= bottom->mx;
  2181. last_mv[2][1]= bottom->my;
  2182. s->m.mb_stride=2;
  2183. s->m.mb_x=
  2184. s->m.mb_y= 0;
  2185. c->skip= 0;
  2186. assert(c-> stride == stride);
  2187. assert(c->uvstride == uvstride);
  2188. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  2189. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  2190. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  2191. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  2192. c->xmin = - x*block_w - 16+3;
  2193. c->ymin = - y*block_w - 16+3;
  2194. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  2195. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  2196. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  2197. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  2198. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  2199. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  2200. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  2201. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  2202. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  2203. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  2204. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  2205. if (!y) {
  2206. c->pred_x= P_LEFT[0];
  2207. c->pred_y= P_LEFT[1];
  2208. } else {
  2209. c->pred_x = P_MEDIAN[0];
  2210. c->pred_y = P_MEDIAN[1];
  2211. }
  2212. score= INT_MAX;
  2213. best_ref= 0;
  2214. for(ref=0; ref<s->ref_frames; ref++){
  2215. init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
  2216. ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  2217. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  2218. assert(ref_mx >= c->xmin);
  2219. assert(ref_mx <= c->xmax);
  2220. assert(ref_my >= c->ymin);
  2221. assert(ref_my <= c->ymax);
  2222. ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  2223. ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  2224. ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  2225. if(s->ref_mvs[ref]){
  2226. s->ref_mvs[ref][index][0]= ref_mx;
  2227. s->ref_mvs[ref][index][1]= ref_my;
  2228. s->ref_scores[ref][index]= ref_score;
  2229. }
  2230. if(score > ref_score){
  2231. score= ref_score;
  2232. best_ref= ref;
  2233. mx= ref_mx;
  2234. my= ref_my;
  2235. }
  2236. }
  2237. //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  2238. // subpel search
  2239. base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
  2240. pc= s->c;
  2241. pc.bytestream_start=
  2242. pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  2243. memcpy(p_state, s->block_state, sizeof(s->block_state));
  2244. if(level!=s->block_max_depth)
  2245. put_rac(&pc, &p_state[4 + s_context], 1);
  2246. put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  2247. if(s->ref_frames > 1)
  2248. put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
  2249. pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  2250. put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
  2251. put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
  2252. p_len= pc.bytestream - pc.bytestream_start;
  2253. score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
  2254. block_s= block_w*block_w;
  2255. sum = pix_sum(current_data[0], stride, block_w);
  2256. l= (sum + block_s/2)/block_s;
  2257. iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  2258. block_s= block_w*block_w>>2;
  2259. sum = pix_sum(current_data[1], uvstride, block_w>>1);
  2260. cb= (sum + block_s/2)/block_s;
  2261. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  2262. sum = pix_sum(current_data[2], uvstride, block_w>>1);
  2263. cr= (sum + block_s/2)/block_s;
  2264. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  2265. ic= s->c;
  2266. ic.bytestream_start=
  2267. ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  2268. memcpy(i_state, s->block_state, sizeof(s->block_state));
  2269. if(level!=s->block_max_depth)
  2270. put_rac(&ic, &i_state[4 + s_context], 1);
  2271. put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  2272. put_symbol(&ic, &i_state[32], l-pl , 1);
  2273. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  2274. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  2275. i_len= ic.bytestream - ic.bytestream_start;
  2276. iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
  2277. // assert(score==256*256*256*64-1);
  2278. assert(iscore < 255*255*256 + s->lambda2*10);
  2279. assert(iscore >= 0);
  2280. assert(l>=0 && l<=255);
  2281. assert(pl>=0 && pl<=255);
  2282. if(level==0){
  2283. int varc= iscore >> 8;
  2284. int vard= score >> 8;
  2285. if (vard <= 64 || vard < varc)
  2286. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  2287. else
  2288. c->scene_change_score+= s->m.qscale;
  2289. }
  2290. if(level!=s->block_max_depth){
  2291. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  2292. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  2293. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  2294. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  2295. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  2296. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  2297. if(score2 < score && score2 < iscore)
  2298. return score2;
  2299. }
  2300. if(iscore < score){
  2301. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2302. memcpy(pbbak, i_buffer, i_len);
  2303. s->c= ic;
  2304. s->c.bytestream_start= pbbak_start;
  2305. s->c.bytestream= pbbak + i_len;
  2306. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  2307. memcpy(s->block_state, i_state, sizeof(s->block_state));
  2308. return iscore;
  2309. }else{
  2310. memcpy(pbbak, p_buffer, p_len);
  2311. s->c= pc;
  2312. s->c.bytestream_start= pbbak_start;
  2313. s->c.bytestream= pbbak + p_len;
  2314. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  2315. memcpy(s->block_state, p_state, sizeof(s->block_state));
  2316. return score;
  2317. }
  2318. }
  2319. static void encode_q_branch2(SnowContext *s, int level, int x, int y){
  2320. const int w= s->b_width << s->block_max_depth;
  2321. const int rem_depth= s->block_max_depth - level;
  2322. const int index= (x + y*w) << rem_depth;
  2323. int trx= (x+1)<<rem_depth;
  2324. BlockNode *b= &s->block[index];
  2325. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2326. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  2327. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  2328. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  2329. int pl = left->color[0];
  2330. int pcb= left->color[1];
  2331. int pcr= left->color[2];
  2332. int pmx, pmy;
  2333. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  2334. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  2335. int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  2336. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  2337. if(s->keyframe){
  2338. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  2339. return;
  2340. }
  2341. if(level!=s->block_max_depth){
  2342. if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  2343. put_rac(&s->c, &s->block_state[4 + s_context], 1);
  2344. }else{
  2345. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  2346. encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  2347. encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  2348. encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  2349. encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  2350. return;
  2351. }
  2352. }
  2353. if(b->type & BLOCK_INTRA){
  2354. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2355. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
  2356. put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
  2357. put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
  2358. put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
  2359. set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  2360. }else{
  2361. pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  2362. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
  2363. if(s->ref_frames > 1)
  2364. put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
  2365. put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
  2366. put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
  2367. set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  2368. }
  2369. }
  2370. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2371. int i, x2, y2;
  2372. Plane *p= &s->plane[plane_index];
  2373. const int block_size = MB_SIZE >> s->block_max_depth;
  2374. const int block_w = plane_index ? block_size/2 : block_size;
  2375. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2376. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2377. const int ref_stride= s->current_picture.linesize[plane_index];
  2378. uint8_t *src= s-> input_picture.data[plane_index];
  2379. IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  2380. const int b_stride = s->b_width << s->block_max_depth;
  2381. const int w= p->width;
  2382. const int h= p->height;
  2383. int index= mb_x + mb_y*b_stride;
  2384. BlockNode *b= &s->block[index];
  2385. BlockNode backup= *b;
  2386. int ab=0;
  2387. int aa=0;
  2388. b->type|= BLOCK_INTRA;
  2389. b->color[plane_index]= 0;
  2390. memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  2391. for(i=0; i<4; i++){
  2392. int mb_x2= mb_x + (i &1) - 1;
  2393. int mb_y2= mb_y + (i>>1) - 1;
  2394. int x= block_w*mb_x2 + block_w/2;
  2395. int y= block_w*mb_y2 + block_w/2;
  2396. add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  2397. x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  2398. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
  2399. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  2400. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
  2401. int obmc_v= obmc[index];
  2402. int d;
  2403. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  2404. if(x<0) obmc_v += obmc[index + block_w];
  2405. if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
  2406. if(x+block_w>w) obmc_v += obmc[index - block_w];
  2407. //FIXME precalculate this or simplify it somehow else
  2408. d = -dst[index] + (1<<(FRAC_BITS-1));
  2409. dst[index] = d;
  2410. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  2411. aa += obmc_v * obmc_v; //FIXME precalculate this
  2412. }
  2413. }
  2414. }
  2415. *b= backup;
  2416. return av_clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we should not need clipping
  2417. }
  2418. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  2419. const int b_stride = s->b_width << s->block_max_depth;
  2420. const int b_height = s->b_height<< s->block_max_depth;
  2421. int index= x + y*b_stride;
  2422. const BlockNode *b = &s->block[index];
  2423. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2424. const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  2425. const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  2426. const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  2427. int dmx, dmy;
  2428. // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2429. // int my_context= av_log2(2*FFABS(left->my - top->my));
  2430. if(x<0 || x>=b_stride || y>=b_height)
  2431. return 0;
  2432. /*
  2433. 1 0 0
  2434. 01X 1-2 1
  2435. 001XX 3-6 2-3
  2436. 0001XXX 7-14 4-7
  2437. 00001XXXX 15-30 8-15
  2438. */
  2439. //FIXME try accurate rate
  2440. //FIXME intra and inter predictors if surrounding blocks are not the same type
  2441. if(b->type & BLOCK_INTRA){
  2442. return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  2443. + av_log2(2*FFABS(left->color[1] - b->color[1]))
  2444. + av_log2(2*FFABS(left->color[2] - b->color[2])));
  2445. }else{
  2446. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  2447. dmx-= b->mx;
  2448. dmy-= b->my;
  2449. return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  2450. + av_log2(2*FFABS(dmy))
  2451. + av_log2(2*b->ref));
  2452. }
  2453. }
  2454. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
  2455. Plane *p= &s->plane[plane_index];
  2456. const int block_size = MB_SIZE >> s->block_max_depth;
  2457. const int block_w = plane_index ? block_size/2 : block_size;
  2458. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2459. const int ref_stride= s->current_picture.linesize[plane_index];
  2460. uint8_t *dst= s->current_picture.data[plane_index];
  2461. uint8_t *src= s-> input_picture.data[plane_index];
  2462. IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2463. uint8_t *cur = s->scratchbuf;
  2464. uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
  2465. const int b_stride = s->b_width << s->block_max_depth;
  2466. const int b_height = s->b_height<< s->block_max_depth;
  2467. const int w= p->width;
  2468. const int h= p->height;
  2469. int distortion;
  2470. int rate= 0;
  2471. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2472. int sx= block_w*mb_x - block_w/2;
  2473. int sy= block_w*mb_y - block_w/2;
  2474. int x0= FFMAX(0,-sx);
  2475. int y0= FFMAX(0,-sy);
  2476. int x1= FFMIN(block_w*2, w-sx);
  2477. int y1= FFMIN(block_w*2, h-sy);
  2478. int i,x,y;
  2479. 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);
  2480. for(y=y0; y<y1; y++){
  2481. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  2482. const IDWTELEM *pred1 = pred + y*obmc_stride;
  2483. uint8_t *cur1 = cur + y*ref_stride;
  2484. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  2485. for(x=x0; x<x1; x++){
  2486. #if FRAC_BITS >= LOG2_OBMC_MAX
  2487. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  2488. #else
  2489. int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  2490. #endif
  2491. v = (v + pred1[x]) >> FRAC_BITS;
  2492. if(v&(~255)) v= ~(v>>31);
  2493. dst1[x] = v;
  2494. }
  2495. }
  2496. /* copy the regions where obmc[] = (uint8_t)256 */
  2497. if(LOG2_OBMC_MAX == 8
  2498. && (mb_x == 0 || mb_x == b_stride-1)
  2499. && (mb_y == 0 || mb_y == b_height-1)){
  2500. if(mb_x == 0)
  2501. x1 = block_w;
  2502. else
  2503. x0 = block_w;
  2504. if(mb_y == 0)
  2505. y1 = block_w;
  2506. else
  2507. y0 = block_w;
  2508. for(y=y0; y<y1; y++)
  2509. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  2510. }
  2511. if(block_w==16){
  2512. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  2513. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  2514. /* FIXME cmps overlap but do not cover the wavelet's whole support.
  2515. * So improving the score of one block is not strictly guaranteed
  2516. * to improve the score of the whole frame, thus iterative motion
  2517. * estimation does not always converge. */
  2518. if(s->avctx->me_cmp == FF_CMP_W97)
  2519. distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2520. else if(s->avctx->me_cmp == FF_CMP_W53)
  2521. distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2522. else{
  2523. distortion = 0;
  2524. for(i=0; i<4; i++){
  2525. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  2526. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  2527. }
  2528. }
  2529. }else{
  2530. assert(block_w==8);
  2531. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  2532. }
  2533. if(plane_index==0){
  2534. for(i=0; i<4; i++){
  2535. /* ..RRr
  2536. * .RXx.
  2537. * rxx..
  2538. */
  2539. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  2540. }
  2541. if(mb_x == b_stride-2)
  2542. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  2543. }
  2544. return distortion + rate*penalty_factor;
  2545. }
  2546. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2547. int i, y2;
  2548. Plane *p= &s->plane[plane_index];
  2549. const int block_size = MB_SIZE >> s->block_max_depth;
  2550. const int block_w = plane_index ? block_size/2 : block_size;
  2551. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2552. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2553. const int ref_stride= s->current_picture.linesize[plane_index];
  2554. uint8_t *dst= s->current_picture.data[plane_index];
  2555. uint8_t *src= s-> input_picture.data[plane_index];
  2556. //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  2557. // const has only been removed from zero_dst to suppress a warning
  2558. static IDWTELEM zero_dst[4096]; //FIXME
  2559. const int b_stride = s->b_width << s->block_max_depth;
  2560. const int w= p->width;
  2561. const int h= p->height;
  2562. int distortion= 0;
  2563. int rate= 0;
  2564. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2565. for(i=0; i<9; i++){
  2566. int mb_x2= mb_x + (i%3) - 1;
  2567. int mb_y2= mb_y + (i/3) - 1;
  2568. int x= block_w*mb_x2 + block_w/2;
  2569. int y= block_w*mb_y2 + block_w/2;
  2570. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  2571. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  2572. //FIXME find a cleaner/simpler way to skip the outside stuff
  2573. for(y2= y; y2<0; y2++)
  2574. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2575. for(y2= h; y2<y+block_w; y2++)
  2576. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2577. if(x<0){
  2578. for(y2= y; y2<y+block_w; y2++)
  2579. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  2580. }
  2581. if(x+block_w > w){
  2582. for(y2= y; y2<y+block_w; y2++)
  2583. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  2584. }
  2585. assert(block_w== 8 || block_w==16);
  2586. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  2587. }
  2588. if(plane_index==0){
  2589. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  2590. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  2591. /* ..RRRr
  2592. * .RXXx.
  2593. * .RXXx.
  2594. * rxxx.
  2595. */
  2596. if(merged)
  2597. rate = get_block_bits(s, mb_x, mb_y, 2);
  2598. for(i=merged?4:0; i<9; i++){
  2599. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  2600. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  2601. }
  2602. }
  2603. return distortion + rate*penalty_factor;
  2604. }
  2605. static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2606. const int w= b->width;
  2607. const int h= b->height;
  2608. int x, y;
  2609. if(1){
  2610. int run=0;
  2611. int runs[w*h];
  2612. int run_index=0;
  2613. int max_index;
  2614. for(y=0; y<h; y++){
  2615. for(x=0; x<w; x++){
  2616. int v, p=0;
  2617. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2618. v= src[x + y*stride];
  2619. if(y){
  2620. t= src[x + (y-1)*stride];
  2621. if(x){
  2622. lt= src[x - 1 + (y-1)*stride];
  2623. }
  2624. if(x + 1 < w){
  2625. rt= src[x + 1 + (y-1)*stride];
  2626. }
  2627. }
  2628. if(x){
  2629. l= src[x - 1 + y*stride];
  2630. /*if(x > 1){
  2631. if(orientation==1) ll= src[y + (x-2)*stride];
  2632. else ll= src[x - 2 + y*stride];
  2633. }*/
  2634. }
  2635. if(parent){
  2636. int px= x>>1;
  2637. int py= y>>1;
  2638. if(px<b->parent->width && py<b->parent->height)
  2639. p= parent[px + py*2*stride];
  2640. }
  2641. if(!(/*ll|*/l|lt|t|rt|p)){
  2642. if(v){
  2643. runs[run_index++]= run;
  2644. run=0;
  2645. }else{
  2646. run++;
  2647. }
  2648. }
  2649. }
  2650. }
  2651. max_index= run_index;
  2652. runs[run_index++]= run;
  2653. run_index=0;
  2654. run= runs[run_index++];
  2655. put_symbol2(&s->c, b->state[30], max_index, 0);
  2656. if(run_index <= max_index)
  2657. put_symbol2(&s->c, b->state[1], run, 3);
  2658. for(y=0; y<h; y++){
  2659. if(s->c.bytestream_end - s->c.bytestream < w*40){
  2660. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2661. return -1;
  2662. }
  2663. for(x=0; x<w; x++){
  2664. int v, p=0;
  2665. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2666. v= src[x + y*stride];
  2667. if(y){
  2668. t= src[x + (y-1)*stride];
  2669. if(x){
  2670. lt= src[x - 1 + (y-1)*stride];
  2671. }
  2672. if(x + 1 < w){
  2673. rt= src[x + 1 + (y-1)*stride];
  2674. }
  2675. }
  2676. if(x){
  2677. l= src[x - 1 + y*stride];
  2678. /*if(x > 1){
  2679. if(orientation==1) ll= src[y + (x-2)*stride];
  2680. else ll= src[x - 2 + y*stride];
  2681. }*/
  2682. }
  2683. if(parent){
  2684. int px= x>>1;
  2685. int py= y>>1;
  2686. if(px<b->parent->width && py<b->parent->height)
  2687. p= parent[px + py*2*stride];
  2688. }
  2689. if(/*ll|*/l|lt|t|rt|p){
  2690. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2691. put_rac(&s->c, &b->state[0][context], !!v);
  2692. }else{
  2693. if(!run){
  2694. run= runs[run_index++];
  2695. if(run_index <= max_index)
  2696. put_symbol2(&s->c, b->state[1], run, 3);
  2697. assert(v);
  2698. }else{
  2699. run--;
  2700. assert(!v);
  2701. }
  2702. }
  2703. if(v){
  2704. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2705. int l2= 2*FFABS(l) + (l<0);
  2706. int t2= 2*FFABS(t) + (t<0);
  2707. put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
  2708. put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
  2709. }
  2710. }
  2711. }
  2712. }
  2713. return 0;
  2714. }
  2715. static int encode_subband(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2716. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  2717. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  2718. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  2719. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  2720. }
  2721. 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){
  2722. const int b_stride= s->b_width << s->block_max_depth;
  2723. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2724. BlockNode backup= *block;
  2725. int rd, index, value;
  2726. assert(mb_x>=0 && mb_y>=0);
  2727. assert(mb_x<b_stride);
  2728. if(intra){
  2729. block->color[0] = p[0];
  2730. block->color[1] = p[1];
  2731. block->color[2] = p[2];
  2732. block->type |= BLOCK_INTRA;
  2733. }else{
  2734. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  2735. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  2736. if(s->me_cache[index] == value)
  2737. return 0;
  2738. s->me_cache[index]= value;
  2739. block->mx= p[0];
  2740. block->my= p[1];
  2741. block->type &= ~BLOCK_INTRA;
  2742. }
  2743. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  2744. //FIXME chroma
  2745. if(rd < *best_rd){
  2746. *best_rd= rd;
  2747. return 1;
  2748. }else{
  2749. *block= backup;
  2750. return 0;
  2751. }
  2752. }
  2753. /* special case for int[2] args we discard afterwards,
  2754. * fixes compilation problem with gcc 2.95 */
  2755. 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){
  2756. int p[2] = {p0, p1};
  2757. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  2758. }
  2759. 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){
  2760. const int b_stride= s->b_width << s->block_max_depth;
  2761. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2762. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  2763. int rd, index, value;
  2764. assert(mb_x>=0 && mb_y>=0);
  2765. assert(mb_x<b_stride);
  2766. assert(((mb_x|mb_y)&1) == 0);
  2767. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  2768. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  2769. if(s->me_cache[index] == value)
  2770. return 0;
  2771. s->me_cache[index]= value;
  2772. block->mx= p0;
  2773. block->my= p1;
  2774. block->ref= ref;
  2775. block->type &= ~BLOCK_INTRA;
  2776. block[1]= block[b_stride]= block[b_stride+1]= *block;
  2777. rd= get_4block_rd(s, mb_x, mb_y, 0);
  2778. //FIXME chroma
  2779. if(rd < *best_rd){
  2780. *best_rd= rd;
  2781. return 1;
  2782. }else{
  2783. block[0]= backup[0];
  2784. block[1]= backup[1];
  2785. block[b_stride]= backup[2];
  2786. block[b_stride+1]= backup[3];
  2787. return 0;
  2788. }
  2789. }
  2790. static void iterative_me(SnowContext *s){
  2791. int pass, mb_x, mb_y;
  2792. const int b_width = s->b_width << s->block_max_depth;
  2793. const int b_height= s->b_height << s->block_max_depth;
  2794. const int b_stride= b_width;
  2795. int color[3];
  2796. {
  2797. RangeCoder r = s->c;
  2798. uint8_t state[sizeof(s->block_state)];
  2799. memcpy(state, s->block_state, sizeof(s->block_state));
  2800. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  2801. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  2802. encode_q_branch(s, 0, mb_x, mb_y);
  2803. s->c = r;
  2804. memcpy(s->block_state, state, sizeof(s->block_state));
  2805. }
  2806. for(pass=0; pass<25; pass++){
  2807. int change= 0;
  2808. for(mb_y= 0; mb_y<b_height; mb_y++){
  2809. for(mb_x= 0; mb_x<b_width; mb_x++){
  2810. int dia_change, i, j, ref;
  2811. int best_rd= INT_MAX, ref_rd;
  2812. BlockNode backup, ref_b;
  2813. const int index= mb_x + mb_y * b_stride;
  2814. BlockNode *block= &s->block[index];
  2815. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  2816. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  2817. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  2818. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  2819. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  2820. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  2821. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  2822. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  2823. const int b_w= (MB_SIZE >> s->block_max_depth);
  2824. uint8_t obmc_edged[b_w*2][b_w*2];
  2825. if(pass && (block->type & BLOCK_OPT))
  2826. continue;
  2827. block->type |= BLOCK_OPT;
  2828. backup= *block;
  2829. if(!s->me_cache_generation)
  2830. memset(s->me_cache, 0, sizeof(s->me_cache));
  2831. s->me_cache_generation += 1<<22;
  2832. //FIXME precalculate
  2833. {
  2834. int x, y;
  2835. memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
  2836. if(mb_x==0)
  2837. for(y=0; y<b_w*2; y++)
  2838. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  2839. if(mb_x==b_stride-1)
  2840. for(y=0; y<b_w*2; y++)
  2841. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  2842. if(mb_y==0){
  2843. for(x=0; x<b_w*2; x++)
  2844. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  2845. for(y=1; y<b_w; y++)
  2846. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  2847. }
  2848. if(mb_y==b_height-1){
  2849. for(x=0; x<b_w*2; x++)
  2850. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  2851. for(y=b_w; y<b_w*2-1; y++)
  2852. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  2853. }
  2854. }
  2855. //skip stuff outside the picture
  2856. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  2857. uint8_t *src= s-> input_picture.data[0];
  2858. uint8_t *dst= s->current_picture.data[0];
  2859. const int stride= s->current_picture.linesize[0];
  2860. const int block_w= MB_SIZE >> s->block_max_depth;
  2861. const int sx= block_w*mb_x - block_w/2;
  2862. const int sy= block_w*mb_y - block_w/2;
  2863. const int w= s->plane[0].width;
  2864. const int h= s->plane[0].height;
  2865. int y;
  2866. for(y=sy; y<0; y++)
  2867. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2868. for(y=h; y<sy+block_w*2; y++)
  2869. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2870. if(sx<0){
  2871. for(y=sy; y<sy+block_w*2; y++)
  2872. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  2873. }
  2874. if(sx+block_w*2 > w){
  2875. for(y=sy; y<sy+block_w*2; y++)
  2876. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  2877. }
  2878. }
  2879. // intra(black) = neighbors' contribution to the current block
  2880. for(i=0; i<3; i++)
  2881. color[i]= get_dc(s, mb_x, mb_y, i);
  2882. // get previous score (cannot be cached due to OBMC)
  2883. if(pass > 0 && (block->type&BLOCK_INTRA)){
  2884. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  2885. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  2886. }else
  2887. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  2888. ref_b= *block;
  2889. ref_rd= best_rd;
  2890. for(ref=0; ref < s->ref_frames; ref++){
  2891. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  2892. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  2893. continue;
  2894. block->ref= ref;
  2895. best_rd= INT_MAX;
  2896. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  2897. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  2898. if(tb)
  2899. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  2900. if(lb)
  2901. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  2902. if(rb)
  2903. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  2904. if(bb)
  2905. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  2906. /* fullpel ME */
  2907. //FIXME avoid subpel interpolation / round to nearest integer
  2908. do{
  2909. dia_change=0;
  2910. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  2911. for(j=0; j<i; j++){
  2912. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2913. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  2914. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  2915. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2916. }
  2917. }
  2918. }while(dia_change);
  2919. /* subpel ME */
  2920. do{
  2921. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  2922. dia_change=0;
  2923. for(i=0; i<8; i++)
  2924. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  2925. }while(dia_change);
  2926. //FIXME or try the standard 2 pass qpel or similar
  2927. mvr[0][0]= block->mx;
  2928. mvr[0][1]= block->my;
  2929. if(ref_rd > best_rd){
  2930. ref_rd= best_rd;
  2931. ref_b= *block;
  2932. }
  2933. }
  2934. best_rd= ref_rd;
  2935. *block= ref_b;
  2936. #if 1
  2937. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  2938. //FIXME RD style color selection
  2939. #endif
  2940. if(!same_block(block, &backup)){
  2941. if(tb ) tb ->type &= ~BLOCK_OPT;
  2942. if(lb ) lb ->type &= ~BLOCK_OPT;
  2943. if(rb ) rb ->type &= ~BLOCK_OPT;
  2944. if(bb ) bb ->type &= ~BLOCK_OPT;
  2945. if(tlb) tlb->type &= ~BLOCK_OPT;
  2946. if(trb) trb->type &= ~BLOCK_OPT;
  2947. if(blb) blb->type &= ~BLOCK_OPT;
  2948. if(brb) brb->type &= ~BLOCK_OPT;
  2949. change ++;
  2950. }
  2951. }
  2952. }
  2953. av_log(s->avctx, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  2954. if(!change)
  2955. break;
  2956. }
  2957. if(s->block_max_depth == 1){
  2958. int change= 0;
  2959. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  2960. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  2961. int i;
  2962. int best_rd, init_rd;
  2963. const int index= mb_x + mb_y * b_stride;
  2964. BlockNode *b[4];
  2965. b[0]= &s->block[index];
  2966. b[1]= b[0]+1;
  2967. b[2]= b[0]+b_stride;
  2968. b[3]= b[2]+1;
  2969. if(same_block(b[0], b[1]) &&
  2970. same_block(b[0], b[2]) &&
  2971. same_block(b[0], b[3]))
  2972. continue;
  2973. if(!s->me_cache_generation)
  2974. memset(s->me_cache, 0, sizeof(s->me_cache));
  2975. s->me_cache_generation += 1<<22;
  2976. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  2977. //FIXME more multiref search?
  2978. check_4block_inter(s, mb_x, mb_y,
  2979. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  2980. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  2981. for(i=0; i<4; i++)
  2982. if(!(b[i]->type&BLOCK_INTRA))
  2983. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  2984. if(init_rd != best_rd)
  2985. change++;
  2986. }
  2987. }
  2988. av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  2989. }
  2990. }
  2991. static void encode_blocks(SnowContext *s, int search){
  2992. int x, y;
  2993. int w= s->b_width;
  2994. int h= s->b_height;
  2995. if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
  2996. iterative_me(s);
  2997. for(y=0; y<h; y++){
  2998. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  2999. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  3000. return;
  3001. }
  3002. for(x=0; x<w; x++){
  3003. if(s->avctx->me_method == ME_ITER || !search)
  3004. encode_q_branch2(s, 0, x, y);
  3005. else
  3006. encode_q_branch (s, 0, x, y);
  3007. }
  3008. }
  3009. }
  3010. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  3011. const int w= b->width;
  3012. const int h= b->height;
  3013. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  3014. const int qmul= qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
  3015. int x,y, thres1, thres2;
  3016. if(s->qlog == LOSSLESS_QLOG){
  3017. for(y=0; y<h; y++)
  3018. for(x=0; x<w; x++)
  3019. dst[x + y*stride]= src[x + y*stride];
  3020. return;
  3021. }
  3022. bias= bias ? 0 : (3*qmul)>>3;
  3023. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  3024. thres2= 2*thres1;
  3025. if(!bias){
  3026. for(y=0; y<h; y++){
  3027. for(x=0; x<w; x++){
  3028. int i= src[x + y*stride];
  3029. if((unsigned)(i+thres1) > thres2){
  3030. if(i>=0){
  3031. i<<= QEXPSHIFT;
  3032. i/= qmul; //FIXME optimize
  3033. dst[x + y*stride]= i;
  3034. }else{
  3035. i= -i;
  3036. i<<= QEXPSHIFT;
  3037. i/= qmul; //FIXME optimize
  3038. dst[x + y*stride]= -i;
  3039. }
  3040. }else
  3041. dst[x + y*stride]= 0;
  3042. }
  3043. }
  3044. }else{
  3045. for(y=0; y<h; y++){
  3046. for(x=0; x<w; x++){
  3047. int i= src[x + y*stride];
  3048. if((unsigned)(i+thres1) > thres2){
  3049. if(i>=0){
  3050. i<<= QEXPSHIFT;
  3051. i= (i + bias) / qmul; //FIXME optimize
  3052. dst[x + y*stride]= i;
  3053. }else{
  3054. i= -i;
  3055. i<<= QEXPSHIFT;
  3056. i= (i + bias) / qmul; //FIXME optimize
  3057. dst[x + y*stride]= -i;
  3058. }
  3059. }else
  3060. dst[x + y*stride]= 0;
  3061. }
  3062. }
  3063. }
  3064. }
  3065. static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
  3066. const int w= b->width;
  3067. const int h= b->height;
  3068. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  3069. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3070. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  3071. int x,y;
  3072. if(s->qlog == LOSSLESS_QLOG) return;
  3073. for(y=0; y<h; y++){
  3074. for(x=0; x<w; x++){
  3075. int i= src[x + y*stride];
  3076. if(i<0){
  3077. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  3078. }else if(i>0){
  3079. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  3080. }
  3081. }
  3082. }
  3083. }
  3084. static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  3085. const int w= b->width;
  3086. const int h= b->height;
  3087. int x,y;
  3088. for(y=h-1; y>=0; y--){
  3089. for(x=w-1; x>=0; x--){
  3090. int i= x + y*stride;
  3091. if(x){
  3092. if(use_median){
  3093. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3094. else src[i] -= src[i - 1];
  3095. }else{
  3096. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3097. else src[i] -= src[i - 1];
  3098. }
  3099. }else{
  3100. if(y) src[i] -= src[i - stride];
  3101. }
  3102. }
  3103. }
  3104. }
  3105. static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  3106. const int w= b->width;
  3107. const int h= b->height;
  3108. int x,y;
  3109. for(y=0; y<h; y++){
  3110. for(x=0; x<w; x++){
  3111. int i= x + y*stride;
  3112. if(x){
  3113. if(use_median){
  3114. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3115. else src[i] += src[i - 1];
  3116. }else{
  3117. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3118. else src[i] += src[i - 1];
  3119. }
  3120. }else{
  3121. if(y) src[i] += src[i - stride];
  3122. }
  3123. }
  3124. }
  3125. }
  3126. static void encode_qlogs(SnowContext *s){
  3127. int plane_index, level, orientation;
  3128. for(plane_index=0; plane_index<2; plane_index++){
  3129. for(level=0; level<s->spatial_decomposition_count; level++){
  3130. for(orientation=level ? 1:0; orientation<4; orientation++){
  3131. if(orientation==2) continue;
  3132. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  3133. }
  3134. }
  3135. }
  3136. }
  3137. static void encode_header(SnowContext *s){
  3138. int plane_index, i;
  3139. uint8_t kstate[32];
  3140. memset(kstate, MID_STATE, sizeof(kstate));
  3141. put_rac(&s->c, kstate, s->keyframe);
  3142. if(s->keyframe || s->always_reset){
  3143. reset_contexts(s);
  3144. s->last_spatial_decomposition_type=
  3145. s->last_qlog=
  3146. s->last_qbias=
  3147. s->last_mv_scale=
  3148. s->last_block_max_depth= 0;
  3149. for(plane_index=0; plane_index<2; plane_index++){
  3150. Plane *p= &s->plane[plane_index];
  3151. p->last_htaps=0;
  3152. p->last_diag_mc=0;
  3153. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  3154. }
  3155. }
  3156. if(s->keyframe){
  3157. put_symbol(&s->c, s->header_state, s->version, 0);
  3158. put_rac(&s->c, s->header_state, s->always_reset);
  3159. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  3160. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  3161. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3162. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  3163. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  3164. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  3165. put_rac(&s->c, s->header_state, s->spatial_scalability);
  3166. // put_rac(&s->c, s->header_state, s->rate_scalability);
  3167. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  3168. encode_qlogs(s);
  3169. }
  3170. if(!s->keyframe){
  3171. int update_mc=0;
  3172. for(plane_index=0; plane_index<2; plane_index++){
  3173. Plane *p= &s->plane[plane_index];
  3174. update_mc |= p->last_htaps != p->htaps;
  3175. update_mc |= p->last_diag_mc != p->diag_mc;
  3176. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  3177. }
  3178. put_rac(&s->c, s->header_state, update_mc);
  3179. if(update_mc){
  3180. for(plane_index=0; plane_index<2; plane_index++){
  3181. Plane *p= &s->plane[plane_index];
  3182. put_rac(&s->c, s->header_state, p->diag_mc);
  3183. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  3184. for(i= p->htaps/2; i; i--)
  3185. put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
  3186. }
  3187. }
  3188. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3189. put_rac(&s->c, s->header_state, 1);
  3190. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3191. encode_qlogs(s);
  3192. }else
  3193. put_rac(&s->c, s->header_state, 0);
  3194. }
  3195. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  3196. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  3197. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  3198. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  3199. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  3200. }
  3201. static void update_last_header_values(SnowContext *s){
  3202. int plane_index;
  3203. if(!s->keyframe){
  3204. for(plane_index=0; plane_index<2; plane_index++){
  3205. Plane *p= &s->plane[plane_index];
  3206. p->last_diag_mc= p->diag_mc;
  3207. p->last_htaps = p->htaps;
  3208. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  3209. }
  3210. }
  3211. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  3212. s->last_qlog = s->qlog;
  3213. s->last_qbias = s->qbias;
  3214. s->last_mv_scale = s->mv_scale;
  3215. s->last_block_max_depth = s->block_max_depth;
  3216. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  3217. }
  3218. static int qscale2qlog(int qscale){
  3219. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  3220. + 61*QROOT/8; //<64 >60
  3221. }
  3222. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  3223. {
  3224. /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
  3225. * FIXME we know exact mv bits at this point,
  3226. * but ratecontrol isn't set up to include them. */
  3227. uint32_t coef_sum= 0;
  3228. int level, orientation, delta_qlog;
  3229. for(level=0; level<s->spatial_decomposition_count; level++){
  3230. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3231. SubBand *b= &s->plane[0].band[level][orientation];
  3232. IDWTELEM *buf= b->ibuf;
  3233. const int w= b->width;
  3234. const int h= b->height;
  3235. const int stride= b->stride;
  3236. const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
  3237. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3238. const int qdiv= (1<<16)/qmul;
  3239. int x, y;
  3240. //FIXME this is ugly
  3241. for(y=0; y<h; y++)
  3242. for(x=0; x<w; x++)
  3243. buf[x+y*stride]= b->buf[x+y*stride];
  3244. if(orientation==0)
  3245. decorrelate(s, b, buf, stride, 1, 0);
  3246. for(y=0; y<h; y++)
  3247. for(x=0; x<w; x++)
  3248. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  3249. }
  3250. }
  3251. /* ugly, ratecontrol just takes a sqrt again */
  3252. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  3253. assert(coef_sum < INT_MAX);
  3254. if(pict->pict_type == FF_I_TYPE){
  3255. s->m.current_picture.mb_var_sum= coef_sum;
  3256. s->m.current_picture.mc_mb_var_sum= 0;
  3257. }else{
  3258. s->m.current_picture.mc_mb_var_sum= coef_sum;
  3259. s->m.current_picture.mb_var_sum= 0;
  3260. }
  3261. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  3262. if (pict->quality < 0)
  3263. return INT_MIN;
  3264. s->lambda= pict->quality * 3/2;
  3265. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  3266. s->qlog+= delta_qlog;
  3267. return delta_qlog;
  3268. }
  3269. static void calculate_visual_weight(SnowContext *s, Plane *p){
  3270. int width = p->width;
  3271. int height= p->height;
  3272. int level, orientation, x, y;
  3273. for(level=0; level<s->spatial_decomposition_count; level++){
  3274. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3275. SubBand *b= &p->band[level][orientation];
  3276. IDWTELEM *ibuf= b->ibuf;
  3277. int64_t error=0;
  3278. memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  3279. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  3280. ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3281. for(y=0; y<height; y++){
  3282. for(x=0; x<width; x++){
  3283. int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  3284. error += d*d;
  3285. }
  3286. }
  3287. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  3288. }
  3289. }
  3290. }
  3291. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  3292. SnowContext *s = avctx->priv_data;
  3293. RangeCoder * const c= &s->c;
  3294. AVFrame *pict = data;
  3295. const int width= s->avctx->width;
  3296. const int height= s->avctx->height;
  3297. int level, orientation, plane_index, i, y;
  3298. uint8_t rc_header_bak[sizeof(s->header_state)];
  3299. uint8_t rc_block_bak[sizeof(s->block_state)];
  3300. ff_init_range_encoder(c, buf, buf_size);
  3301. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3302. for(i=0; i<3; i++){
  3303. int shift= !!i;
  3304. for(y=0; y<(height>>shift); y++)
  3305. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  3306. &pict->data[i][y * pict->linesize[i]],
  3307. width>>shift);
  3308. }
  3309. s->new_picture = *pict;
  3310. s->m.picture_number= avctx->frame_number;
  3311. if(avctx->flags&CODEC_FLAG_PASS2){
  3312. s->m.pict_type =
  3313. pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  3314. s->keyframe= pict->pict_type==FF_I_TYPE;
  3315. if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
  3316. pict->quality= ff_rate_estimate_qscale(&s->m, 0);
  3317. if (pict->quality < 0)
  3318. return -1;
  3319. }
  3320. }else{
  3321. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  3322. s->m.pict_type=
  3323. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  3324. }
  3325. if(s->pass1_rc && avctx->frame_number == 0)
  3326. pict->quality= 2*FF_QP2LAMBDA;
  3327. if(pict->quality){
  3328. s->qlog= qscale2qlog(pict->quality);
  3329. s->lambda = pict->quality * 3/2;
  3330. }
  3331. if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
  3332. s->qlog= LOSSLESS_QLOG;
  3333. s->lambda = 0;
  3334. }//else keep previous frame's qlog until after motion estimation
  3335. frame_start(s);
  3336. s->m.current_picture_ptr= &s->m.current_picture;
  3337. s->m.last_picture.pts= s->m.current_picture.pts;
  3338. s->m.current_picture.pts= pict->pts;
  3339. if(pict->pict_type == FF_P_TYPE){
  3340. int block_width = (width +15)>>4;
  3341. int block_height= (height+15)>>4;
  3342. int stride= s->current_picture.linesize[0];
  3343. assert(s->current_picture.data[0]);
  3344. assert(s->last_picture[0].data[0]);
  3345. s->m.avctx= s->avctx;
  3346. s->m.current_picture.data[0]= s->current_picture.data[0];
  3347. s->m. last_picture.data[0]= s->last_picture[0].data[0];
  3348. s->m. new_picture.data[0]= s-> input_picture.data[0];
  3349. s->m. last_picture_ptr= &s->m. last_picture;
  3350. s->m.linesize=
  3351. s->m. last_picture.linesize[0]=
  3352. s->m. new_picture.linesize[0]=
  3353. s->m.current_picture.linesize[0]= stride;
  3354. s->m.uvlinesize= s->current_picture.linesize[1];
  3355. s->m.width = width;
  3356. s->m.height= height;
  3357. s->m.mb_width = block_width;
  3358. s->m.mb_height= block_height;
  3359. s->m.mb_stride= s->m.mb_width+1;
  3360. s->m.b8_stride= 2*s->m.mb_width+1;
  3361. s->m.f_code=1;
  3362. s->m.pict_type= pict->pict_type;
  3363. s->m.me_method= s->avctx->me_method;
  3364. s->m.me.scene_change_score=0;
  3365. s->m.flags= s->avctx->flags;
  3366. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  3367. s->m.out_format= FMT_H263;
  3368. s->m.unrestricted_mv= 1;
  3369. s->m.lambda = s->lambda;
  3370. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  3371. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  3372. s->m.dsp= s->dsp; //move
  3373. ff_init_me(&s->m);
  3374. s->dsp= s->m.dsp;
  3375. }
  3376. if(s->pass1_rc){
  3377. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  3378. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  3379. }
  3380. redo_frame:
  3381. if(pict->pict_type == FF_I_TYPE)
  3382. s->spatial_decomposition_count= 5;
  3383. else
  3384. s->spatial_decomposition_count= 5;
  3385. s->m.pict_type = pict->pict_type;
  3386. s->qbias= pict->pict_type == FF_P_TYPE ? 2 : 0;
  3387. common_init_after_header(avctx);
  3388. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3389. for(plane_index=0; plane_index<3; plane_index++){
  3390. calculate_visual_weight(s, &s->plane[plane_index]);
  3391. }
  3392. }
  3393. encode_header(s);
  3394. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3395. encode_blocks(s, 1);
  3396. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  3397. for(plane_index=0; plane_index<3; plane_index++){
  3398. Plane *p= &s->plane[plane_index];
  3399. int w= p->width;
  3400. int h= p->height;
  3401. int x, y;
  3402. // int bits= put_bits_count(&s->c.pb);
  3403. if(!(avctx->flags2 & CODEC_FLAG2_MEMC_ONLY)){
  3404. //FIXME optimize
  3405. if(pict->data[plane_index]) //FIXME gray hack
  3406. for(y=0; y<h; y++){
  3407. for(x=0; x<w; x++){
  3408. s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3409. }
  3410. }
  3411. predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  3412. if( plane_index==0
  3413. && pict->pict_type == FF_P_TYPE
  3414. && !(avctx->flags&CODEC_FLAG_PASS2)
  3415. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3416. ff_init_range_encoder(c, buf, buf_size);
  3417. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3418. pict->pict_type= FF_I_TYPE;
  3419. s->keyframe=1;
  3420. s->current_picture.key_frame=1;
  3421. goto redo_frame;
  3422. }
  3423. if(s->qlog == LOSSLESS_QLOG){
  3424. for(y=0; y<h; y++){
  3425. for(x=0; x<w; x++){
  3426. s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  3427. }
  3428. }
  3429. }else{
  3430. for(y=0; y<h; y++){
  3431. for(x=0; x<w; x++){
  3432. s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  3433. }
  3434. }
  3435. }
  3436. /* if(QUANTIZE2)
  3437. dwt_quantize(s, p, s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type);
  3438. else*/
  3439. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3440. if(s->pass1_rc && plane_index==0){
  3441. int delta_qlog = ratecontrol_1pass(s, pict);
  3442. if (delta_qlog <= INT_MIN)
  3443. return -1;
  3444. if(delta_qlog){
  3445. //reordering qlog in the bitstream would eliminate this reset
  3446. ff_init_range_encoder(c, buf, buf_size);
  3447. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  3448. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  3449. encode_header(s);
  3450. encode_blocks(s, 0);
  3451. }
  3452. }
  3453. for(level=0; level<s->spatial_decomposition_count; level++){
  3454. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3455. SubBand *b= &p->band[level][orientation];
  3456. if(!QUANTIZE2)
  3457. quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
  3458. if(orientation==0)
  3459. decorrelate(s, b, b->ibuf, b->stride, pict->pict_type == FF_P_TYPE, 0);
  3460. encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
  3461. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3462. if(orientation==0)
  3463. correlate(s, b, b->ibuf, b->stride, 1, 0);
  3464. }
  3465. }
  3466. for(level=0; level<s->spatial_decomposition_count; level++){
  3467. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3468. SubBand *b= &p->band[level][orientation];
  3469. dequantize(s, b, b->ibuf, b->stride);
  3470. }
  3471. }
  3472. ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3473. if(s->qlog == LOSSLESS_QLOG){
  3474. for(y=0; y<h; y++){
  3475. for(x=0; x<w; x++){
  3476. s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  3477. }
  3478. }
  3479. }
  3480. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3481. }else{
  3482. //ME/MC only
  3483. if(pict->pict_type == FF_I_TYPE){
  3484. for(y=0; y<h; y++){
  3485. for(x=0; x<w; x++){
  3486. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  3487. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3488. }
  3489. }
  3490. }else{
  3491. memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  3492. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3493. }
  3494. }
  3495. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3496. int64_t error= 0;
  3497. if(pict->data[plane_index]) //FIXME gray hack
  3498. for(y=0; y<h; y++){
  3499. for(x=0; x<w; x++){
  3500. 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];
  3501. error += d*d;
  3502. }
  3503. }
  3504. s->avctx->error[plane_index] += error;
  3505. s->current_picture.error[plane_index] = error;
  3506. }
  3507. }
  3508. update_last_header_values(s);
  3509. release_buffer(avctx);
  3510. s->current_picture.coded_picture_number = avctx->frame_number;
  3511. s->current_picture.pict_type = pict->pict_type;
  3512. s->current_picture.quality = pict->quality;
  3513. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3514. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  3515. s->m.current_picture.display_picture_number =
  3516. s->m.current_picture.coded_picture_number = avctx->frame_number;
  3517. s->m.current_picture.quality = pict->quality;
  3518. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  3519. if(s->pass1_rc)
  3520. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  3521. return -1;
  3522. if(avctx->flags&CODEC_FLAG_PASS1)
  3523. ff_write_pass1_stats(&s->m);
  3524. s->m.last_pict_type = s->m.pict_type;
  3525. avctx->frame_bits = s->m.frame_bits;
  3526. avctx->mv_bits = s->m.mv_bits;
  3527. avctx->misc_bits = s->m.misc_bits;
  3528. avctx->p_tex_bits = s->m.p_tex_bits;
  3529. emms_c();
  3530. return ff_rac_terminate(c);
  3531. }
  3532. static av_cold int encode_end(AVCodecContext *avctx)
  3533. {
  3534. SnowContext *s = avctx->priv_data;
  3535. common_end(s);
  3536. if (s->input_picture.data[0])
  3537. avctx->release_buffer(avctx, &s->input_picture);
  3538. av_free(avctx->stats_out);
  3539. return 0;
  3540. }
  3541. AVCodec snow_encoder = {
  3542. "snow",
  3543. AVMEDIA_TYPE_VIDEO,
  3544. CODEC_ID_SNOW,
  3545. sizeof(SnowContext),
  3546. encode_init,
  3547. encode_frame,
  3548. encode_end,
  3549. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  3550. };
  3551. #endif
  3552. #ifdef TEST
  3553. #undef malloc
  3554. #undef free
  3555. #undef printf
  3556. #include "libavutil/lfg.h"
  3557. int main(void){
  3558. int width=256;
  3559. int height=256;
  3560. int buffer[2][width*height];
  3561. SnowContext s;
  3562. int i;
  3563. AVLFG prng;
  3564. s.spatial_decomposition_count=6;
  3565. s.spatial_decomposition_type=1;
  3566. av_lfg_init(&prng, 1);
  3567. printf("testing 5/3 DWT\n");
  3568. for(i=0; i<width*height; i++)
  3569. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  3570. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3571. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3572. for(i=0; i<width*height; i++)
  3573. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  3574. printf("testing 9/7 DWT\n");
  3575. s.spatial_decomposition_type=0;
  3576. for(i=0; i<width*height; i++)
  3577. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  3578. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3579. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3580. for(i=0; i<width*height; i++)
  3581. if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  3582. #if 0
  3583. printf("testing AC coder\n");
  3584. memset(s.header_state, 0, sizeof(s.header_state));
  3585. ff_init_range_encoder(&s.c, buffer[0], 256*256);
  3586. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3587. for(i=-256; i<256; i++){
  3588. put_symbol(&s.c, s.header_state, i*i*i/3*FFABS(i), 1);
  3589. }
  3590. ff_rac_terminate(&s.c);
  3591. memset(s.header_state, 0, sizeof(s.header_state));
  3592. ff_init_range_decoder(&s.c, buffer[0], 256*256);
  3593. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3594. for(i=-256; i<256; i++){
  3595. int j;
  3596. j= get_symbol(&s.c, s.header_state, 1);
  3597. if(j!=i*i*i/3*FFABS(i)) printf("fsck: %d != %d\n", i, j);
  3598. }
  3599. #endif
  3600. {
  3601. int level, orientation, x, y;
  3602. int64_t errors[8][4];
  3603. int64_t g=0;
  3604. memset(errors, 0, sizeof(errors));
  3605. s.spatial_decomposition_count=3;
  3606. s.spatial_decomposition_type=0;
  3607. for(level=0; level<s.spatial_decomposition_count; level++){
  3608. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3609. int w= width >> (s.spatial_decomposition_count-level);
  3610. int h= height >> (s.spatial_decomposition_count-level);
  3611. int stride= width << (s.spatial_decomposition_count-level);
  3612. DWTELEM *buf= buffer[0];
  3613. int64_t error=0;
  3614. if(orientation&1) buf+=w;
  3615. if(orientation>1) buf+=stride>>1;
  3616. memset(buffer[0], 0, sizeof(int)*width*height);
  3617. buf[w/2 + h/2*stride]= 256*256;
  3618. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3619. for(y=0; y<height; y++){
  3620. for(x=0; x<width; x++){
  3621. int64_t d= buffer[0][x + y*width];
  3622. error += d*d;
  3623. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
  3624. }
  3625. if(FFABS(height/2-y)<9 && level==2) printf("\n");
  3626. }
  3627. error= (int)(sqrt(error)+0.5);
  3628. errors[level][orientation]= error;
  3629. if(g) g=av_gcd(g, error);
  3630. else g= error;
  3631. }
  3632. }
  3633. printf("static int const visual_weight[][4]={\n");
  3634. for(level=0; level<s.spatial_decomposition_count; level++){
  3635. printf(" {");
  3636. for(orientation=0; orientation<4; orientation++){
  3637. printf("%8"PRId64",", errors[level][orientation]/g);
  3638. }
  3639. printf("},\n");
  3640. }
  3641. printf("};\n");
  3642. {
  3643. int level=2;
  3644. int w= width >> (s.spatial_decomposition_count-level);
  3645. //int h= height >> (s.spatial_decomposition_count-level);
  3646. int stride= width << (s.spatial_decomposition_count-level);
  3647. DWTELEM *buf= buffer[0];
  3648. int64_t error=0;
  3649. buf+=w;
  3650. buf+=stride>>1;
  3651. memset(buffer[0], 0, sizeof(int)*width*height);
  3652. #if 1
  3653. for(y=0; y<height; y++){
  3654. for(x=0; x<width; x++){
  3655. int tab[4]={0,2,3,1};
  3656. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  3657. }
  3658. }
  3659. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3660. #else
  3661. for(y=0; y<h; y++){
  3662. for(x=0; x<w; x++){
  3663. buf[x + y*stride ]=169;
  3664. buf[x + y*stride-w]=64;
  3665. }
  3666. }
  3667. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3668. #endif
  3669. for(y=0; y<height; y++){
  3670. for(x=0; x<width; x++){
  3671. int64_t d= buffer[0][x + y*width];
  3672. error += d*d;
  3673. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
  3674. }
  3675. if(FFABS(height/2-y)<9) printf("\n");
  3676. }
  3677. }
  3678. }
  3679. return 0;
  3680. }
  3681. #endif /* TEST */