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.

4151 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, uint8_t *tmp, 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. uint8_t tmp[stride*(b_w+HTAPS_MAX-1)];\
  1011. assert(h==b_w);\
  1012. mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, tmp, stride, b_w, b_w, dx, dy);\
  1013. }
  1014. mca( 0, 0,16)
  1015. mca( 8, 0,16)
  1016. mca( 0, 8,16)
  1017. mca( 8, 8,16)
  1018. mca( 0, 0,8)
  1019. mca( 8, 0,8)
  1020. mca( 0, 8,8)
  1021. mca( 8, 8,8)
  1022. 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){
  1023. if(block->type & BLOCK_INTRA){
  1024. int x, y;
  1025. const int color = block->color[plane_index];
  1026. const int color4= color*0x01010101;
  1027. if(b_w==32){
  1028. for(y=0; y < b_h; y++){
  1029. *(uint32_t*)&dst[0 + y*stride]= color4;
  1030. *(uint32_t*)&dst[4 + y*stride]= color4;
  1031. *(uint32_t*)&dst[8 + y*stride]= color4;
  1032. *(uint32_t*)&dst[12+ y*stride]= color4;
  1033. *(uint32_t*)&dst[16+ y*stride]= color4;
  1034. *(uint32_t*)&dst[20+ y*stride]= color4;
  1035. *(uint32_t*)&dst[24+ y*stride]= color4;
  1036. *(uint32_t*)&dst[28+ y*stride]= color4;
  1037. }
  1038. }else if(b_w==16){
  1039. for(y=0; y < b_h; y++){
  1040. *(uint32_t*)&dst[0 + y*stride]= color4;
  1041. *(uint32_t*)&dst[4 + y*stride]= color4;
  1042. *(uint32_t*)&dst[8 + y*stride]= color4;
  1043. *(uint32_t*)&dst[12+ y*stride]= color4;
  1044. }
  1045. }else if(b_w==8){
  1046. for(y=0; y < b_h; y++){
  1047. *(uint32_t*)&dst[0 + y*stride]= color4;
  1048. *(uint32_t*)&dst[4 + y*stride]= color4;
  1049. }
  1050. }else if(b_w==4){
  1051. for(y=0; y < b_h; y++){
  1052. *(uint32_t*)&dst[0 + y*stride]= color4;
  1053. }
  1054. }else{
  1055. for(y=0; y < b_h; y++){
  1056. for(x=0; x < b_w; x++){
  1057. dst[x + y*stride]= color;
  1058. }
  1059. }
  1060. }
  1061. }else{
  1062. uint8_t *src= s->last_picture[block->ref].data[plane_index];
  1063. const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
  1064. int mx= block->mx*scale;
  1065. int my= block->my*scale;
  1066. const int dx= mx&15;
  1067. const int dy= my&15;
  1068. const int tab_index= 3 - (b_w>>2) + (b_w>>4);
  1069. sx += (mx>>4) - (HTAPS_MAX/2-1);
  1070. sy += (my>>4) - (HTAPS_MAX/2-1);
  1071. src += sx + sy*stride;
  1072. if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)
  1073. || (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){
  1074. ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);
  1075. src= tmp + MB_SIZE;
  1076. }
  1077. // assert(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h);
  1078. // assert(!(b_w&(b_w-1)));
  1079. assert(b_w>1 && b_h>1);
  1080. assert((tab_index>=0 && tab_index<4) || b_w==32);
  1081. 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 )
  1082. mc_block(&s->plane[plane_index], dst, src, tmp, stride, b_w, b_h, dx, dy);
  1083. else if(b_w==32){
  1084. int y;
  1085. for(y=0; y<b_h; y+=16){
  1086. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
  1087. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
  1088. }
  1089. }else if(b_w==b_h)
  1090. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
  1091. else if(b_w==2*b_h){
  1092. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
  1093. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
  1094. }else{
  1095. assert(2*b_w==b_h);
  1096. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
  1097. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
  1098. }
  1099. }
  1100. }
  1101. void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
  1102. int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
  1103. int y, x;
  1104. IDWTELEM * dst;
  1105. for(y=0; y<b_h; y++){
  1106. //FIXME ugly misuse of obmc_stride
  1107. const uint8_t *obmc1= obmc + y*obmc_stride;
  1108. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  1109. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  1110. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  1111. dst = slice_buffer_get_line(sb, src_y + y);
  1112. for(x=0; x<b_w; x++){
  1113. int v= obmc1[x] * block[3][x + y*src_stride]
  1114. +obmc2[x] * block[2][x + y*src_stride]
  1115. +obmc3[x] * block[1][x + y*src_stride]
  1116. +obmc4[x] * block[0][x + y*src_stride];
  1117. v <<= 8 - LOG2_OBMC_MAX;
  1118. if(FRAC_BITS != 8){
  1119. v >>= 8 - FRAC_BITS;
  1120. }
  1121. if(add){
  1122. v += dst[x + src_x];
  1123. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  1124. if(v&(~255)) v= ~(v>>31);
  1125. dst8[x + y*src_stride] = v;
  1126. }else{
  1127. dst[x + src_x] -= v;
  1128. }
  1129. }
  1130. }
  1131. }
  1132. //FIXME name cleanup (b_w, block_w, b_width stuff)
  1133. 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){
  1134. const int b_width = s->b_width << s->block_max_depth;
  1135. const int b_height= s->b_height << s->block_max_depth;
  1136. const int b_stride= b_width;
  1137. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  1138. BlockNode *rt= lt+1;
  1139. BlockNode *lb= lt+b_stride;
  1140. BlockNode *rb= lb+1;
  1141. uint8_t *block[4];
  1142. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  1143. uint8_t *tmp = s->scratchbuf;
  1144. uint8_t *ptmp;
  1145. int x,y;
  1146. if(b_x<0){
  1147. lt= rt;
  1148. lb= rb;
  1149. }else if(b_x + 1 >= b_width){
  1150. rt= lt;
  1151. rb= lb;
  1152. }
  1153. if(b_y<0){
  1154. lt= lb;
  1155. rt= rb;
  1156. }else if(b_y + 1 >= b_height){
  1157. lb= lt;
  1158. rb= rt;
  1159. }
  1160. if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
  1161. obmc -= src_x;
  1162. b_w += src_x;
  1163. if(!sliced && !offset_dst)
  1164. dst -= src_x;
  1165. src_x=0;
  1166. }else if(src_x + b_w > w){
  1167. b_w = w - src_x;
  1168. }
  1169. if(src_y<0){
  1170. obmc -= src_y*obmc_stride;
  1171. b_h += src_y;
  1172. if(!sliced && !offset_dst)
  1173. dst -= src_y*dst_stride;
  1174. src_y=0;
  1175. }else if(src_y + b_h> h){
  1176. b_h = h - src_y;
  1177. }
  1178. if(b_w<=0 || b_h<=0) return;
  1179. assert(src_stride > 2*MB_SIZE + 5);
  1180. if(!sliced && offset_dst)
  1181. dst += src_x + src_y*dst_stride;
  1182. dst8+= src_x + src_y*src_stride;
  1183. // src += src_x + src_y*src_stride;
  1184. ptmp= tmp + 3*tmp_step;
  1185. block[0]= ptmp;
  1186. ptmp+=tmp_step;
  1187. pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  1188. if(same_block(lt, rt)){
  1189. block[1]= block[0];
  1190. }else{
  1191. block[1]= ptmp;
  1192. ptmp+=tmp_step;
  1193. pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  1194. }
  1195. if(same_block(lt, lb)){
  1196. block[2]= block[0];
  1197. }else if(same_block(rt, lb)){
  1198. block[2]= block[1];
  1199. }else{
  1200. block[2]= ptmp;
  1201. ptmp+=tmp_step;
  1202. pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  1203. }
  1204. if(same_block(lt, rb) ){
  1205. block[3]= block[0];
  1206. }else if(same_block(rt, rb)){
  1207. block[3]= block[1];
  1208. }else if(same_block(lb, rb)){
  1209. block[3]= block[2];
  1210. }else{
  1211. block[3]= ptmp;
  1212. pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  1213. }
  1214. #if 0
  1215. for(y=0; y<b_h; y++){
  1216. for(x=0; x<b_w; x++){
  1217. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  1218. if(add) dst[x + y*dst_stride] += v;
  1219. else dst[x + y*dst_stride] -= v;
  1220. }
  1221. }
  1222. for(y=0; y<b_h; y++){
  1223. uint8_t *obmc2= obmc + (obmc_stride>>1);
  1224. for(x=0; x<b_w; x++){
  1225. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  1226. if(add) dst[x + y*dst_stride] += v;
  1227. else dst[x + y*dst_stride] -= v;
  1228. }
  1229. }
  1230. for(y=0; y<b_h; y++){
  1231. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  1232. for(x=0; x<b_w; x++){
  1233. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  1234. if(add) dst[x + y*dst_stride] += v;
  1235. else dst[x + y*dst_stride] -= v;
  1236. }
  1237. }
  1238. for(y=0; y<b_h; y++){
  1239. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  1240. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  1241. for(x=0; x<b_w; x++){
  1242. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  1243. if(add) dst[x + y*dst_stride] += v;
  1244. else dst[x + y*dst_stride] -= v;
  1245. }
  1246. }
  1247. #else
  1248. if(sliced){
  1249. s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  1250. }else{
  1251. for(y=0; y<b_h; y++){
  1252. //FIXME ugly misuse of obmc_stride
  1253. const uint8_t *obmc1= obmc + y*obmc_stride;
  1254. const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  1255. const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  1256. const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  1257. for(x=0; x<b_w; x++){
  1258. int v= obmc1[x] * block[3][x + y*src_stride]
  1259. +obmc2[x] * block[2][x + y*src_stride]
  1260. +obmc3[x] * block[1][x + y*src_stride]
  1261. +obmc4[x] * block[0][x + y*src_stride];
  1262. v <<= 8 - LOG2_OBMC_MAX;
  1263. if(FRAC_BITS != 8){
  1264. v >>= 8 - FRAC_BITS;
  1265. }
  1266. if(add){
  1267. v += dst[x + y*dst_stride];
  1268. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  1269. if(v&(~255)) v= ~(v>>31);
  1270. dst8[x + y*src_stride] = v;
  1271. }else{
  1272. dst[x + y*dst_stride] -= v;
  1273. }
  1274. }
  1275. }
  1276. }
  1277. #endif /* 0 */
  1278. }
  1279. static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
  1280. Plane *p= &s->plane[plane_index];
  1281. const int mb_w= s->b_width << s->block_max_depth;
  1282. const int mb_h= s->b_height << s->block_max_depth;
  1283. int x, y, mb_x;
  1284. int block_size = MB_SIZE >> s->block_max_depth;
  1285. int block_w = plane_index ? block_size/2 : block_size;
  1286. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  1287. int obmc_stride= plane_index ? block_size : 2*block_size;
  1288. int ref_stride= s->current_picture.linesize[plane_index];
  1289. uint8_t *dst8= s->current_picture.data[plane_index];
  1290. int w= p->width;
  1291. int h= p->height;
  1292. if(s->keyframe || (s->avctx->debug&512)){
  1293. if(mb_y==mb_h)
  1294. return;
  1295. if(add){
  1296. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  1297. // DWTELEM * line = slice_buffer_get_line(sb, y);
  1298. IDWTELEM * line = sb->line[y];
  1299. for(x=0; x<w; x++){
  1300. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  1301. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  1302. v >>= FRAC_BITS;
  1303. if(v&(~255)) v= ~(v>>31);
  1304. dst8[x + y*ref_stride]= v;
  1305. }
  1306. }
  1307. }else{
  1308. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  1309. // DWTELEM * line = slice_buffer_get_line(sb, y);
  1310. IDWTELEM * line = sb->line[y];
  1311. for(x=0; x<w; x++){
  1312. line[x] -= 128 << FRAC_BITS;
  1313. // buf[x + y*w]-= 128<<FRAC_BITS;
  1314. }
  1315. }
  1316. }
  1317. return;
  1318. }
  1319. for(mb_x=0; mb_x<=mb_w; mb_x++){
  1320. add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  1321. block_w*mb_x - block_w/2,
  1322. block_w*mb_y - block_w/2,
  1323. block_w, block_w,
  1324. w, h,
  1325. w, ref_stride, obmc_stride,
  1326. mb_x - 1, mb_y - 1,
  1327. add, 0, plane_index);
  1328. }
  1329. }
  1330. static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
  1331. Plane *p= &s->plane[plane_index];
  1332. const int mb_w= s->b_width << s->block_max_depth;
  1333. const int mb_h= s->b_height << s->block_max_depth;
  1334. int x, y, mb_x;
  1335. int block_size = MB_SIZE >> s->block_max_depth;
  1336. int block_w = plane_index ? block_size/2 : block_size;
  1337. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  1338. const int obmc_stride= plane_index ? block_size : 2*block_size;
  1339. int ref_stride= s->current_picture.linesize[plane_index];
  1340. uint8_t *dst8= s->current_picture.data[plane_index];
  1341. int w= p->width;
  1342. int h= p->height;
  1343. if(s->keyframe || (s->avctx->debug&512)){
  1344. if(mb_y==mb_h)
  1345. return;
  1346. if(add){
  1347. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  1348. for(x=0; x<w; x++){
  1349. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  1350. v >>= FRAC_BITS;
  1351. if(v&(~255)) v= ~(v>>31);
  1352. dst8[x + y*ref_stride]= v;
  1353. }
  1354. }
  1355. }else{
  1356. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  1357. for(x=0; x<w; x++){
  1358. buf[x + y*w]-= 128<<FRAC_BITS;
  1359. }
  1360. }
  1361. }
  1362. return;
  1363. }
  1364. for(mb_x=0; mb_x<=mb_w; mb_x++){
  1365. add_yblock(s, 0, NULL, buf, dst8, obmc,
  1366. block_w*mb_x - block_w/2,
  1367. block_w*mb_y - block_w/2,
  1368. block_w, block_w,
  1369. w, h,
  1370. w, ref_stride, obmc_stride,
  1371. mb_x - 1, mb_y - 1,
  1372. add, 1, plane_index);
  1373. }
  1374. }
  1375. static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
  1376. const int mb_h= s->b_height << s->block_max_depth;
  1377. int mb_y;
  1378. for(mb_y=0; mb_y<=mb_h; mb_y++)
  1379. predict_slice(s, buf, plane_index, add, mb_y);
  1380. }
  1381. static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
  1382. const int w= b->width;
  1383. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  1384. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  1385. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1386. int x,y;
  1387. if(s->qlog == LOSSLESS_QLOG) return;
  1388. for(y=start_y; y<end_y; y++){
  1389. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  1390. IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  1391. for(x=0; x<w; x++){
  1392. int i= line[x];
  1393. if(i<0){
  1394. line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  1395. }else if(i>0){
  1396. line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
  1397. }
  1398. }
  1399. }
  1400. }
  1401. 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){
  1402. const int w= b->width;
  1403. int x,y;
  1404. IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning
  1405. IDWTELEM * prev;
  1406. if (start_y != 0)
  1407. line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  1408. for(y=start_y; y<end_y; y++){
  1409. prev = line;
  1410. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  1411. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  1412. for(x=0; x<w; x++){
  1413. if(x){
  1414. if(use_median){
  1415. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  1416. else line[x] += line[x - 1];
  1417. }else{
  1418. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  1419. else line[x] += line[x - 1];
  1420. }
  1421. }else{
  1422. if(y) line[x] += prev[x];
  1423. }
  1424. }
  1425. }
  1426. }
  1427. static void decode_qlogs(SnowContext *s){
  1428. int plane_index, level, orientation;
  1429. for(plane_index=0; plane_index<3; plane_index++){
  1430. for(level=0; level<s->spatial_decomposition_count; level++){
  1431. for(orientation=level ? 1:0; orientation<4; orientation++){
  1432. int q;
  1433. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  1434. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  1435. else q= get_symbol(&s->c, s->header_state, 1);
  1436. s->plane[plane_index].band[level][orientation].qlog= q;
  1437. }
  1438. }
  1439. }
  1440. }
  1441. #define GET_S(dst, check) \
  1442. tmp= get_symbol(&s->c, s->header_state, 0);\
  1443. if(!(check)){\
  1444. av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
  1445. return -1;\
  1446. }\
  1447. dst= tmp;
  1448. static int decode_header(SnowContext *s){
  1449. int plane_index, tmp;
  1450. uint8_t kstate[32];
  1451. memset(kstate, MID_STATE, sizeof(kstate));
  1452. s->keyframe= get_rac(&s->c, kstate);
  1453. if(s->keyframe || s->always_reset){
  1454. reset_contexts(s);
  1455. s->spatial_decomposition_type=
  1456. s->qlog=
  1457. s->qbias=
  1458. s->mv_scale=
  1459. s->block_max_depth= 0;
  1460. }
  1461. if(s->keyframe){
  1462. GET_S(s->version, tmp <= 0U)
  1463. s->always_reset= get_rac(&s->c, s->header_state);
  1464. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  1465. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  1466. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  1467. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  1468. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  1469. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  1470. s->spatial_scalability= get_rac(&s->c, s->header_state);
  1471. // s->rate_scalability= get_rac(&s->c, s->header_state);
  1472. GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
  1473. s->max_ref_frames++;
  1474. decode_qlogs(s);
  1475. }
  1476. if(!s->keyframe){
  1477. if(get_rac(&s->c, s->header_state)){
  1478. for(plane_index=0; plane_index<2; plane_index++){
  1479. int htaps, i, sum=0;
  1480. Plane *p= &s->plane[plane_index];
  1481. p->diag_mc= get_rac(&s->c, s->header_state);
  1482. htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
  1483. if((unsigned)htaps > HTAPS_MAX || htaps==0)
  1484. return -1;
  1485. p->htaps= htaps;
  1486. for(i= htaps/2; i; i--){
  1487. p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1));
  1488. sum += p->hcoeff[i];
  1489. }
  1490. p->hcoeff[0]= 32-sum;
  1491. }
  1492. s->plane[2].diag_mc= s->plane[1].diag_mc;
  1493. s->plane[2].htaps = s->plane[1].htaps;
  1494. memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
  1495. }
  1496. if(get_rac(&s->c, s->header_state)){
  1497. GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
  1498. decode_qlogs(s);
  1499. }
  1500. }
  1501. s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
  1502. if(s->spatial_decomposition_type > 1U){
  1503. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  1504. return -1;
  1505. }
  1506. if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
  1507. s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 0){
  1508. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size", s->spatial_decomposition_count);
  1509. return -1;
  1510. }
  1511. s->qlog += get_symbol(&s->c, s->header_state, 1);
  1512. s->mv_scale += get_symbol(&s->c, s->header_state, 1);
  1513. s->qbias += get_symbol(&s->c, s->header_state, 1);
  1514. s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
  1515. if(s->block_max_depth > 1 || s->block_max_depth < 0){
  1516. av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
  1517. s->block_max_depth= 0;
  1518. return -1;
  1519. }
  1520. return 0;
  1521. }
  1522. static void init_qexp(void){
  1523. int i;
  1524. double v=128;
  1525. for(i=0; i<QROOT; i++){
  1526. qexp[i]= lrintf(v);
  1527. v *= pow(2, 1.0 / QROOT);
  1528. }
  1529. }
  1530. static av_cold int common_init(AVCodecContext *avctx){
  1531. SnowContext *s = avctx->priv_data;
  1532. int width, height;
  1533. int i, j;
  1534. s->avctx= avctx;
  1535. s->max_ref_frames=1; //just make sure its not an invalid value in case of no initial keyframe
  1536. dsputil_init(&s->dsp, avctx);
  1537. ff_dwt_init(&s->dwt);
  1538. #define mcf(dx,dy)\
  1539. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  1540. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  1541. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  1542. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  1543. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  1544. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  1545. mcf( 0, 0)
  1546. mcf( 4, 0)
  1547. mcf( 8, 0)
  1548. mcf(12, 0)
  1549. mcf( 0, 4)
  1550. mcf( 4, 4)
  1551. mcf( 8, 4)
  1552. mcf(12, 4)
  1553. mcf( 0, 8)
  1554. mcf( 4, 8)
  1555. mcf( 8, 8)
  1556. mcf(12, 8)
  1557. mcf( 0,12)
  1558. mcf( 4,12)
  1559. mcf( 8,12)
  1560. mcf(12,12)
  1561. #define mcfh(dx,dy)\
  1562. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  1563. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  1564. mc_block_hpel ## dx ## dy ## 16;\
  1565. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  1566. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  1567. mc_block_hpel ## dx ## dy ## 8;
  1568. mcfh(0, 0)
  1569. mcfh(8, 0)
  1570. mcfh(0, 8)
  1571. mcfh(8, 8)
  1572. if(!qexp[0])
  1573. init_qexp();
  1574. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  1575. width= s->avctx->width;
  1576. height= s->avctx->height;
  1577. s->spatial_idwt_buffer= av_mallocz(width*height*sizeof(IDWTELEM));
  1578. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM)); //FIXME this does not belong here
  1579. for(i=0; i<MAX_REF_FRAMES; i++)
  1580. for(j=0; j<MAX_REF_FRAMES; j++)
  1581. scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  1582. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  1583. s->scratchbuf = av_malloc(s->mconly_picture.linesize[0]*7*MB_SIZE);
  1584. return 0;
  1585. }
  1586. static int common_init_after_header(AVCodecContext *avctx){
  1587. SnowContext *s = avctx->priv_data;
  1588. int plane_index, level, orientation;
  1589. for(plane_index=0; plane_index<3; plane_index++){
  1590. int w= s->avctx->width;
  1591. int h= s->avctx->height;
  1592. if(plane_index){
  1593. w>>= s->chroma_h_shift;
  1594. h>>= s->chroma_v_shift;
  1595. }
  1596. s->plane[plane_index].width = w;
  1597. s->plane[plane_index].height= h;
  1598. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  1599. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1600. SubBand *b= &s->plane[plane_index].band[level][orientation];
  1601. b->buf= s->spatial_dwt_buffer;
  1602. b->level= level;
  1603. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  1604. b->width = (w + !(orientation&1))>>1;
  1605. b->height= (h + !(orientation>1))>>1;
  1606. b->stride_line = 1 << (s->spatial_decomposition_count - level);
  1607. b->buf_x_offset = 0;
  1608. b->buf_y_offset = 0;
  1609. if(orientation&1){
  1610. b->buf += (w+1)>>1;
  1611. b->buf_x_offset = (w+1)>>1;
  1612. }
  1613. if(orientation>1){
  1614. b->buf += b->stride>>1;
  1615. b->buf_y_offset = b->stride_line >> 1;
  1616. }
  1617. b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
  1618. if(level)
  1619. b->parent= &s->plane[plane_index].band[level-1][orientation];
  1620. //FIXME avoid this realloc
  1621. av_freep(&b->x_coeff);
  1622. b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
  1623. }
  1624. w= (w+1)>>1;
  1625. h= (h+1)>>1;
  1626. }
  1627. }
  1628. return 0;
  1629. }
  1630. #define QUANTIZE2 0
  1631. #if QUANTIZE2==1
  1632. #define Q2_STEP 8
  1633. static void find_sse(SnowContext *s, Plane *p, int *score, int score_stride, IDWTELEM *r0, IDWTELEM *r1, int level, int orientation){
  1634. SubBand *b= &p->band[level][orientation];
  1635. int x, y;
  1636. int xo=0;
  1637. int yo=0;
  1638. int step= 1 << (s->spatial_decomposition_count - level);
  1639. if(orientation&1)
  1640. xo= step>>1;
  1641. if(orientation&2)
  1642. yo= step>>1;
  1643. //FIXME bias for nonzero ?
  1644. //FIXME optimize
  1645. memset(score, 0, sizeof(*score)*score_stride*((p->height + Q2_STEP-1)/Q2_STEP));
  1646. for(y=0; y<p->height; y++){
  1647. for(x=0; x<p->width; x++){
  1648. int sx= (x-xo + step/2) / step / Q2_STEP;
  1649. int sy= (y-yo + step/2) / step / Q2_STEP;
  1650. int v= r0[x + y*p->width] - r1[x + y*p->width];
  1651. assert(sx>=0 && sy>=0 && sx < score_stride);
  1652. v= ((v+8)>>4)<<4;
  1653. score[sx + sy*score_stride] += v*v;
  1654. assert(score[sx + sy*score_stride] >= 0);
  1655. }
  1656. }
  1657. }
  1658. static void dequantize_all(SnowContext *s, Plane *p, IDWTELEM *buffer, int width, int height){
  1659. int level, orientation;
  1660. for(level=0; level<s->spatial_decomposition_count; level++){
  1661. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1662. SubBand *b= &p->band[level][orientation];
  1663. IDWTELEM *dst= buffer + (b->ibuf - s->spatial_idwt_buffer);
  1664. dequantize(s, b, dst, b->stride);
  1665. }
  1666. }
  1667. }
  1668. static void dwt_quantize(SnowContext *s, Plane *p, DWTELEM *buffer, int width, int height, int stride, int type){
  1669. int level, orientation, ys, xs, x, y, pass;
  1670. IDWTELEM best_dequant[height * stride];
  1671. IDWTELEM idwt2_buffer[height * stride];
  1672. const int score_stride= (width + 10)/Q2_STEP;
  1673. int best_score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
  1674. int score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
  1675. int threshold= (s->m.lambda * s->m.lambda) >> 6;
  1676. //FIXME pass the copy cleanly ?
  1677. // memcpy(dwt_buffer, buffer, height * stride * sizeof(DWTELEM));
  1678. ff_spatial_dwt(buffer, width, height, stride, type, s->spatial_decomposition_count);
  1679. for(level=0; level<s->spatial_decomposition_count; level++){
  1680. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1681. SubBand *b= &p->band[level][orientation];
  1682. IDWTELEM *dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
  1683. DWTELEM *src= buffer + (b-> buf - s->spatial_dwt_buffer);
  1684. assert(src == b->buf); // code does not depend on this but it is true currently
  1685. quantize(s, b, dst, src, b->stride, s->qbias);
  1686. }
  1687. }
  1688. for(pass=0; pass<1; pass++){
  1689. if(s->qbias == 0) //keyframe
  1690. continue;
  1691. for(level=0; level<s->spatial_decomposition_count; level++){
  1692. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1693. SubBand *b= &p->band[level][orientation];
  1694. IDWTELEM *dst= idwt2_buffer + (b->ibuf - s->spatial_idwt_buffer);
  1695. IDWTELEM *best_dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
  1696. for(ys= 0; ys<Q2_STEP; ys++){
  1697. for(xs= 0; xs<Q2_STEP; xs++){
  1698. memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
  1699. dequantize_all(s, p, idwt2_buffer, width, height);
  1700. ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
  1701. find_sse(s, p, best_score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
  1702. memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
  1703. for(y=ys; y<b->height; y+= Q2_STEP){
  1704. for(x=xs; x<b->width; x+= Q2_STEP){
  1705. if(dst[x + y*b->stride]<0) dst[x + y*b->stride]++;
  1706. if(dst[x + y*b->stride]>0) dst[x + y*b->stride]--;
  1707. //FIXME try more than just --
  1708. }
  1709. }
  1710. dequantize_all(s, p, idwt2_buffer, width, height);
  1711. ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
  1712. find_sse(s, p, score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
  1713. for(y=ys; y<b->height; y+= Q2_STEP){
  1714. for(x=xs; x<b->width; x+= Q2_STEP){
  1715. int score_idx= x/Q2_STEP + (y/Q2_STEP)*score_stride;
  1716. if(score[score_idx] <= best_score[score_idx] + threshold){
  1717. best_score[score_idx]= score[score_idx];
  1718. if(best_dst[x + y*b->stride]<0) best_dst[x + y*b->stride]++;
  1719. if(best_dst[x + y*b->stride]>0) best_dst[x + y*b->stride]--;
  1720. //FIXME copy instead
  1721. }
  1722. }
  1723. }
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. memcpy(s->spatial_idwt_buffer, best_dequant, height * stride * sizeof(IDWTELEM)); //FIXME work with that directly instead of copy at the end
  1730. }
  1731. #endif /* QUANTIZE2==1 */
  1732. #define USE_HALFPEL_PLANE 0
  1733. static void halfpel_interpol(SnowContext *s, uint8_t *halfpel[4][4], AVFrame *frame){
  1734. int p,x,y;
  1735. assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));
  1736. for(p=0; p<3; p++){
  1737. int is_chroma= !!p;
  1738. int w= s->avctx->width >>is_chroma;
  1739. int h= s->avctx->height >>is_chroma;
  1740. int ls= frame->linesize[p];
  1741. uint8_t *src= frame->data[p];
  1742. halfpel[1][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  1743. halfpel[2][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  1744. halfpel[3][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
  1745. halfpel[0][p]= src;
  1746. for(y=0; y<h; y++){
  1747. for(x=0; x<w; x++){
  1748. int i= y*ls + x;
  1749. 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;
  1750. }
  1751. }
  1752. for(y=0; y<h; y++){
  1753. for(x=0; x<w; x++){
  1754. int i= y*ls + x;
  1755. 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;
  1756. }
  1757. }
  1758. src= halfpel[1][p];
  1759. for(y=0; y<h; y++){
  1760. for(x=0; x<w; x++){
  1761. int i= y*ls + x;
  1762. 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;
  1763. }
  1764. }
  1765. //FIXME border!
  1766. }
  1767. }
  1768. static void release_buffer(AVCodecContext *avctx){
  1769. SnowContext *s = avctx->priv_data;
  1770. int i;
  1771. if(s->last_picture[s->max_ref_frames-1].data[0]){
  1772. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  1773. for(i=0; i<9; i++)
  1774. if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3])
  1775. av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture.linesize[i%3]));
  1776. }
  1777. }
  1778. static int frame_start(SnowContext *s){
  1779. AVFrame tmp;
  1780. int w= s->avctx->width; //FIXME round up to x16 ?
  1781. int h= s->avctx->height;
  1782. if(s->current_picture.data[0]){
  1783. s->dsp.draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  1784. s->dsp.draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  1785. s->dsp.draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  1786. }
  1787. release_buffer(s->avctx);
  1788. tmp= s->last_picture[s->max_ref_frames-1];
  1789. memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
  1790. memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
  1791. if(USE_HALFPEL_PLANE && s->current_picture.data[0])
  1792. halfpel_interpol(s, s->halfpel_plane[0], &s->current_picture);
  1793. s->last_picture[0]= s->current_picture;
  1794. s->current_picture= tmp;
  1795. if(s->keyframe){
  1796. s->ref_frames= 0;
  1797. }else{
  1798. int i;
  1799. for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
  1800. if(i && s->last_picture[i-1].key_frame)
  1801. break;
  1802. s->ref_frames= i;
  1803. if(s->ref_frames==0){
  1804. av_log(s->avctx,AV_LOG_ERROR, "No reference frames\n");
  1805. return -1;
  1806. }
  1807. }
  1808. s->current_picture.reference= 1;
  1809. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  1810. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1811. return -1;
  1812. }
  1813. s->current_picture.key_frame= s->keyframe;
  1814. return 0;
  1815. }
  1816. static av_cold void common_end(SnowContext *s){
  1817. int plane_index, level, orientation, i;
  1818. av_freep(&s->spatial_dwt_buffer);
  1819. av_freep(&s->spatial_idwt_buffer);
  1820. s->m.me.temp= NULL;
  1821. av_freep(&s->m.me.scratchpad);
  1822. av_freep(&s->m.me.map);
  1823. av_freep(&s->m.me.score_map);
  1824. av_freep(&s->m.obmc_scratchpad);
  1825. av_freep(&s->block);
  1826. av_freep(&s->scratchbuf);
  1827. for(i=0; i<MAX_REF_FRAMES; i++){
  1828. av_freep(&s->ref_mvs[i]);
  1829. av_freep(&s->ref_scores[i]);
  1830. if(s->last_picture[i].data[0])
  1831. s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
  1832. }
  1833. for(plane_index=0; plane_index<3; plane_index++){
  1834. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  1835. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1836. SubBand *b= &s->plane[plane_index].band[level][orientation];
  1837. av_freep(&b->x_coeff);
  1838. }
  1839. }
  1840. }
  1841. if (s->mconly_picture.data[0])
  1842. s->avctx->release_buffer(s->avctx, &s->mconly_picture);
  1843. if (s->current_picture.data[0])
  1844. s->avctx->release_buffer(s->avctx, &s->current_picture);
  1845. }
  1846. static av_cold int decode_init(AVCodecContext *avctx)
  1847. {
  1848. avctx->pix_fmt= PIX_FMT_YUV420P;
  1849. common_init(avctx);
  1850. return 0;
  1851. }
  1852. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt){
  1853. const uint8_t *buf = avpkt->data;
  1854. int buf_size = avpkt->size;
  1855. SnowContext *s = avctx->priv_data;
  1856. RangeCoder * const c= &s->c;
  1857. int bytes_read;
  1858. AVFrame *picture = data;
  1859. int level, orientation, plane_index;
  1860. ff_init_range_decoder(c, buf, buf_size);
  1861. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  1862. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  1863. if(decode_header(s)<0)
  1864. return -1;
  1865. common_init_after_header(avctx);
  1866. // realloc slice buffer for the case that spatial_decomposition_count changed
  1867. ff_slice_buffer_destroy(&s->sb);
  1868. 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);
  1869. for(plane_index=0; plane_index<3; plane_index++){
  1870. Plane *p= &s->plane[plane_index];
  1871. p->fast_mc= p->diag_mc && p->htaps==6 && p->hcoeff[0]==40
  1872. && p->hcoeff[1]==-10
  1873. && p->hcoeff[2]==2;
  1874. }
  1875. alloc_blocks(s);
  1876. if(frame_start(s) < 0)
  1877. return -1;
  1878. //keyframe flag duplication mess FIXME
  1879. if(avctx->debug&FF_DEBUG_PICT_INFO)
  1880. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  1881. decode_blocks(s);
  1882. for(plane_index=0; plane_index<3; plane_index++){
  1883. Plane *p= &s->plane[plane_index];
  1884. int w= p->width;
  1885. int h= p->height;
  1886. int x, y;
  1887. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  1888. if(s->avctx->debug&2048){
  1889. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  1890. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  1891. for(y=0; y<h; y++){
  1892. for(x=0; x<w; x++){
  1893. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  1894. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  1895. }
  1896. }
  1897. }
  1898. {
  1899. for(level=0; level<s->spatial_decomposition_count; level++){
  1900. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1901. SubBand *b= &p->band[level][orientation];
  1902. unpack_coeffs(s, b, b->parent, orientation);
  1903. }
  1904. }
  1905. }
  1906. {
  1907. const int mb_h= s->b_height << s->block_max_depth;
  1908. const int block_size = MB_SIZE >> s->block_max_depth;
  1909. const int block_w = plane_index ? block_size/2 : block_size;
  1910. int mb_y;
  1911. DWTCompose cs[MAX_DECOMPOSITIONS];
  1912. int yd=0, yq=0;
  1913. int y;
  1914. int end_y;
  1915. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  1916. for(mb_y=0; mb_y<=mb_h; mb_y++){
  1917. int slice_starty = block_w*mb_y;
  1918. int slice_h = block_w*(mb_y+1);
  1919. if (!(s->keyframe || s->avctx->debug&512)){
  1920. slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
  1921. slice_h -= (block_w >> 1);
  1922. }
  1923. for(level=0; level<s->spatial_decomposition_count; level++){
  1924. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  1925. SubBand *b= &p->band[level][orientation];
  1926. int start_y;
  1927. int end_y;
  1928. int our_mb_start = mb_y;
  1929. int our_mb_end = (mb_y + 1);
  1930. const int extra= 3;
  1931. start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  1932. end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  1933. if (!(s->keyframe || s->avctx->debug&512)){
  1934. start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  1935. end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  1936. }
  1937. start_y = FFMIN(b->height, start_y);
  1938. end_y = FFMIN(b->height, end_y);
  1939. if (start_y != end_y){
  1940. if (orientation == 0){
  1941. SubBand * correlate_band = &p->band[0][0];
  1942. int correlate_end_y = FFMIN(b->height, end_y + 1);
  1943. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  1944. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  1945. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  1946. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->ibuf, correlate_band->stride, start_y, end_y);
  1947. }
  1948. else
  1949. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  1950. }
  1951. }
  1952. }
  1953. for(; yd<slice_h; yd+=4){
  1954. ff_spatial_idwt_buffered_slice(&s->dwt, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  1955. }
  1956. if(s->qlog == LOSSLESS_QLOG){
  1957. for(; yq<slice_h && yq<h; yq++){
  1958. IDWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  1959. for(x=0; x<w; x++){
  1960. line[x] <<= FRAC_BITS;
  1961. }
  1962. }
  1963. }
  1964. predict_slice_buffered(s, &s->sb, s->spatial_idwt_buffer, plane_index, 1, mb_y);
  1965. y = FFMIN(p->height, slice_starty);
  1966. end_y = FFMIN(p->height, slice_h);
  1967. while(y < end_y)
  1968. ff_slice_buffer_release(&s->sb, y++);
  1969. }
  1970. ff_slice_buffer_flush(&s->sb);
  1971. }
  1972. }
  1973. emms_c();
  1974. release_buffer(avctx);
  1975. if(!(s->avctx->debug&2048))
  1976. *picture= s->current_picture;
  1977. else
  1978. *picture= s->mconly_picture;
  1979. *data_size = sizeof(AVFrame);
  1980. bytes_read= c->bytestream - c->bytestream_start;
  1981. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  1982. return bytes_read;
  1983. }
  1984. static av_cold int decode_end(AVCodecContext *avctx)
  1985. {
  1986. SnowContext *s = avctx->priv_data;
  1987. ff_slice_buffer_destroy(&s->sb);
  1988. common_end(s);
  1989. return 0;
  1990. }
  1991. AVCodec snow_decoder = {
  1992. "snow",
  1993. CODEC_TYPE_VIDEO,
  1994. CODEC_ID_SNOW,
  1995. sizeof(SnowContext),
  1996. decode_init,
  1997. NULL,
  1998. decode_end,
  1999. decode_frame,
  2000. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  2001. NULL,
  2002. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  2003. };
  2004. #if CONFIG_SNOW_ENCODER
  2005. static av_cold int encode_init(AVCodecContext *avctx)
  2006. {
  2007. SnowContext *s = avctx->priv_data;
  2008. int plane_index;
  2009. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  2010. av_log(avctx, AV_LOG_ERROR, "This codec is under development, files encoded with it may not be decodable with future versions!!!\n"
  2011. "Use vstrict=-2 / -strict -2 to use it anyway.\n");
  2012. return -1;
  2013. }
  2014. if(avctx->prediction_method == DWT_97
  2015. && (avctx->flags & CODEC_FLAG_QSCALE)
  2016. && avctx->global_quality == 0){
  2017. av_log(avctx, AV_LOG_ERROR, "The 9/7 wavelet is incompatible with lossless mode.\n");
  2018. return -1;
  2019. }
  2020. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  2021. s->mv_scale = (avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  2022. s->block_max_depth= (avctx->flags & CODEC_FLAG_4MV ) ? 1 : 0;
  2023. for(plane_index=0; plane_index<3; plane_index++){
  2024. s->plane[plane_index].diag_mc= 1;
  2025. s->plane[plane_index].htaps= 6;
  2026. s->plane[plane_index].hcoeff[0]= 40;
  2027. s->plane[plane_index].hcoeff[1]= -10;
  2028. s->plane[plane_index].hcoeff[2]= 2;
  2029. s->plane[plane_index].fast_mc= 1;
  2030. }
  2031. common_init(avctx);
  2032. alloc_blocks(s);
  2033. s->version=0;
  2034. s->m.avctx = avctx;
  2035. s->m.flags = avctx->flags;
  2036. s->m.bit_rate= avctx->bit_rate;
  2037. s->m.me.temp =
  2038. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  2039. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2040. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2041. s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  2042. h263_encode_init(&s->m); //mv_penalty
  2043. s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
  2044. if(avctx->flags&CODEC_FLAG_PASS1){
  2045. if(!avctx->stats_out)
  2046. avctx->stats_out = av_mallocz(256);
  2047. }
  2048. if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  2049. if(ff_rate_control_init(&s->m) < 0)
  2050. return -1;
  2051. }
  2052. s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  2053. avctx->coded_frame= &s->current_picture;
  2054. switch(avctx->pix_fmt){
  2055. // case PIX_FMT_YUV444P:
  2056. // case PIX_FMT_YUV422P:
  2057. case PIX_FMT_YUV420P:
  2058. case PIX_FMT_GRAY8:
  2059. // case PIX_FMT_YUV411P:
  2060. // case PIX_FMT_YUV410P:
  2061. s->colorspace_type= 0;
  2062. break;
  2063. /* case PIX_FMT_RGB32:
  2064. s->colorspace= 1;
  2065. break;*/
  2066. default:
  2067. av_log(avctx, AV_LOG_ERROR, "pixel format not supported\n");
  2068. return -1;
  2069. }
  2070. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  2071. s->chroma_h_shift= 1;
  2072. s->chroma_v_shift= 1;
  2073. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  2074. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  2075. s->avctx->get_buffer(s->avctx, &s->input_picture);
  2076. if(s->avctx->me_method == ME_ITER){
  2077. int i;
  2078. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  2079. for(i=0; i<s->max_ref_frames; i++){
  2080. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  2081. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  2082. }
  2083. }
  2084. return 0;
  2085. }
  2086. //near copy & paste from dsputil, FIXME
  2087. static int pix_sum(uint8_t * pix, int line_size, int w)
  2088. {
  2089. int s, i, j;
  2090. s = 0;
  2091. for (i = 0; i < w; i++) {
  2092. for (j = 0; j < w; j++) {
  2093. s += pix[0];
  2094. pix ++;
  2095. }
  2096. pix += line_size - w;
  2097. }
  2098. return s;
  2099. }
  2100. //near copy & paste from dsputil, FIXME
  2101. static int pix_norm1(uint8_t * pix, int line_size, int w)
  2102. {
  2103. int s, i, j;
  2104. uint32_t *sq = ff_squareTbl + 256;
  2105. s = 0;
  2106. for (i = 0; i < w; i++) {
  2107. for (j = 0; j < w; j ++) {
  2108. s += sq[pix[0]];
  2109. pix ++;
  2110. }
  2111. pix += line_size - w;
  2112. }
  2113. return s;
  2114. }
  2115. //FIXME copy&paste
  2116. #define P_LEFT P[1]
  2117. #define P_TOP P[2]
  2118. #define P_TOPRIGHT P[3]
  2119. #define P_MEDIAN P[4]
  2120. #define P_MV1 P[9]
  2121. #define FLAG_QPEL 1 //must be 1
  2122. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  2123. uint8_t p_buffer[1024];
  2124. uint8_t i_buffer[1024];
  2125. uint8_t p_state[sizeof(s->block_state)];
  2126. uint8_t i_state[sizeof(s->block_state)];
  2127. RangeCoder pc, ic;
  2128. uint8_t *pbbak= s->c.bytestream;
  2129. uint8_t *pbbak_start= s->c.bytestream_start;
  2130. int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
  2131. const int w= s->b_width << s->block_max_depth;
  2132. const int h= s->b_height << s->block_max_depth;
  2133. const int rem_depth= s->block_max_depth - level;
  2134. const int index= (x + y*w) << rem_depth;
  2135. const int block_w= 1<<(LOG2_MB_SIZE - level);
  2136. int trx= (x+1)<<rem_depth;
  2137. int try= (y+1)<<rem_depth;
  2138. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2139. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  2140. const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  2141. const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  2142. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  2143. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  2144. int pl = left->color[0];
  2145. int pcb= left->color[1];
  2146. int pcr= left->color[2];
  2147. int pmx, pmy;
  2148. int mx=0, my=0;
  2149. int l,cr,cb;
  2150. const int stride= s->current_picture.linesize[0];
  2151. const int uvstride= s->current_picture.linesize[1];
  2152. uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y* stride)*block_w,
  2153. s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
  2154. s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
  2155. int P[10][2];
  2156. int16_t last_mv[3][2];
  2157. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  2158. const int shift= 1+qpel;
  2159. MotionEstContext *c= &s->m.me;
  2160. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  2161. int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2162. int my_context= av_log2(2*FFABS(left->my - top->my));
  2163. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  2164. int ref, best_ref, ref_score, ref_mx, ref_my;
  2165. assert(sizeof(s->block_state) >= 256);
  2166. if(s->keyframe){
  2167. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  2168. return 0;
  2169. }
  2170. // clip predictors / edge ?
  2171. P_LEFT[0]= left->mx;
  2172. P_LEFT[1]= left->my;
  2173. P_TOP [0]= top->mx;
  2174. P_TOP [1]= top->my;
  2175. P_TOPRIGHT[0]= tr->mx;
  2176. P_TOPRIGHT[1]= tr->my;
  2177. last_mv[0][0]= s->block[index].mx;
  2178. last_mv[0][1]= s->block[index].my;
  2179. last_mv[1][0]= right->mx;
  2180. last_mv[1][1]= right->my;
  2181. last_mv[2][0]= bottom->mx;
  2182. last_mv[2][1]= bottom->my;
  2183. s->m.mb_stride=2;
  2184. s->m.mb_x=
  2185. s->m.mb_y= 0;
  2186. c->skip= 0;
  2187. assert(c-> stride == stride);
  2188. assert(c->uvstride == uvstride);
  2189. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  2190. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  2191. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  2192. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  2193. c->xmin = - x*block_w - 16+3;
  2194. c->ymin = - y*block_w - 16+3;
  2195. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  2196. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-3;
  2197. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  2198. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  2199. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  2200. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  2201. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  2202. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  2203. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  2204. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  2205. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  2206. if (!y) {
  2207. c->pred_x= P_LEFT[0];
  2208. c->pred_y= P_LEFT[1];
  2209. } else {
  2210. c->pred_x = P_MEDIAN[0];
  2211. c->pred_y = P_MEDIAN[1];
  2212. }
  2213. score= INT_MAX;
  2214. best_ref= 0;
  2215. for(ref=0; ref<s->ref_frames; ref++){
  2216. init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
  2217. ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
  2218. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  2219. assert(ref_mx >= c->xmin);
  2220. assert(ref_mx <= c->xmax);
  2221. assert(ref_my >= c->ymin);
  2222. assert(ref_my <= c->ymax);
  2223. ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  2224. ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  2225. ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
  2226. if(s->ref_mvs[ref]){
  2227. s->ref_mvs[ref][index][0]= ref_mx;
  2228. s->ref_mvs[ref][index][1]= ref_my;
  2229. s->ref_scores[ref][index]= ref_score;
  2230. }
  2231. if(score > ref_score){
  2232. score= ref_score;
  2233. best_ref= ref;
  2234. mx= ref_mx;
  2235. my= ref_my;
  2236. }
  2237. }
  2238. //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
  2239. // subpel search
  2240. base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
  2241. pc= s->c;
  2242. pc.bytestream_start=
  2243. pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  2244. memcpy(p_state, s->block_state, sizeof(s->block_state));
  2245. if(level!=s->block_max_depth)
  2246. put_rac(&pc, &p_state[4 + s_context], 1);
  2247. put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  2248. if(s->ref_frames > 1)
  2249. put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
  2250. pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
  2251. put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
  2252. put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
  2253. p_len= pc.bytestream - pc.bytestream_start;
  2254. score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
  2255. block_s= block_w*block_w;
  2256. sum = pix_sum(current_data[0], stride, block_w);
  2257. l= (sum + block_s/2)/block_s;
  2258. iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
  2259. block_s= block_w*block_w>>2;
  2260. sum = pix_sum(current_data[1], uvstride, block_w>>1);
  2261. cb= (sum + block_s/2)/block_s;
  2262. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  2263. sum = pix_sum(current_data[2], uvstride, block_w>>1);
  2264. cr= (sum + block_s/2)/block_s;
  2265. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  2266. ic= s->c;
  2267. ic.bytestream_start=
  2268. ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  2269. memcpy(i_state, s->block_state, sizeof(s->block_state));
  2270. if(level!=s->block_max_depth)
  2271. put_rac(&ic, &i_state[4 + s_context], 1);
  2272. put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  2273. put_symbol(&ic, &i_state[32], l-pl , 1);
  2274. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  2275. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  2276. i_len= ic.bytestream - ic.bytestream_start;
  2277. iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
  2278. // assert(score==256*256*256*64-1);
  2279. assert(iscore < 255*255*256 + s->lambda2*10);
  2280. assert(iscore >= 0);
  2281. assert(l>=0 && l<=255);
  2282. assert(pl>=0 && pl<=255);
  2283. if(level==0){
  2284. int varc= iscore >> 8;
  2285. int vard= score >> 8;
  2286. if (vard <= 64 || vard < varc)
  2287. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  2288. else
  2289. c->scene_change_score+= s->m.qscale;
  2290. }
  2291. if(level!=s->block_max_depth){
  2292. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  2293. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  2294. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  2295. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  2296. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  2297. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  2298. if(score2 < score && score2 < iscore)
  2299. return score2;
  2300. }
  2301. if(iscore < score){
  2302. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2303. memcpy(pbbak, i_buffer, i_len);
  2304. s->c= ic;
  2305. s->c.bytestream_start= pbbak_start;
  2306. s->c.bytestream= pbbak + i_len;
  2307. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
  2308. memcpy(s->block_state, i_state, sizeof(s->block_state));
  2309. return iscore;
  2310. }else{
  2311. memcpy(pbbak, p_buffer, p_len);
  2312. s->c= pc;
  2313. s->c.bytestream_start= pbbak_start;
  2314. s->c.bytestream= pbbak + p_len;
  2315. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
  2316. memcpy(s->block_state, p_state, sizeof(s->block_state));
  2317. return score;
  2318. }
  2319. }
  2320. static void encode_q_branch2(SnowContext *s, int level, int x, int y){
  2321. const int w= s->b_width << s->block_max_depth;
  2322. const int rem_depth= s->block_max_depth - level;
  2323. const int index= (x + y*w) << rem_depth;
  2324. int trx= (x+1)<<rem_depth;
  2325. BlockNode *b= &s->block[index];
  2326. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2327. const BlockNode *top = y ? &s->block[index-w] : &null_block;
  2328. const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  2329. const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  2330. int pl = left->color[0];
  2331. int pcb= left->color[1];
  2332. int pcr= left->color[2];
  2333. int pmx, pmy;
  2334. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  2335. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
  2336. int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
  2337. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  2338. if(s->keyframe){
  2339. set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
  2340. return;
  2341. }
  2342. if(level!=s->block_max_depth){
  2343. if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
  2344. put_rac(&s->c, &s->block_state[4 + s_context], 1);
  2345. }else{
  2346. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  2347. encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
  2348. encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
  2349. encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
  2350. encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
  2351. return;
  2352. }
  2353. }
  2354. if(b->type & BLOCK_INTRA){
  2355. pred_mv(s, &pmx, &pmy, 0, left, top, tr);
  2356. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
  2357. put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
  2358. put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
  2359. put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
  2360. set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
  2361. }else{
  2362. pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
  2363. put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
  2364. if(s->ref_frames > 1)
  2365. put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
  2366. put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
  2367. put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
  2368. set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
  2369. }
  2370. }
  2371. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2372. int i, x2, y2;
  2373. Plane *p= &s->plane[plane_index];
  2374. const int block_size = MB_SIZE >> s->block_max_depth;
  2375. const int block_w = plane_index ? block_size/2 : block_size;
  2376. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2377. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2378. const int ref_stride= s->current_picture.linesize[plane_index];
  2379. uint8_t *src= s-> input_picture.data[plane_index];
  2380. IDWTELEM *dst= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4; //FIXME change to unsigned
  2381. const int b_stride = s->b_width << s->block_max_depth;
  2382. const int w= p->width;
  2383. const int h= p->height;
  2384. int index= mb_x + mb_y*b_stride;
  2385. BlockNode *b= &s->block[index];
  2386. BlockNode backup= *b;
  2387. int ab=0;
  2388. int aa=0;
  2389. b->type|= BLOCK_INTRA;
  2390. b->color[plane_index]= 0;
  2391. memset(dst, 0, obmc_stride*obmc_stride*sizeof(IDWTELEM));
  2392. for(i=0; i<4; i++){
  2393. int mb_x2= mb_x + (i &1) - 1;
  2394. int mb_y2= mb_y + (i>>1) - 1;
  2395. int x= block_w*mb_x2 + block_w/2;
  2396. int y= block_w*mb_y2 + block_w/2;
  2397. add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  2398. x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  2399. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
  2400. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  2401. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
  2402. int obmc_v= obmc[index];
  2403. int d;
  2404. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  2405. if(x<0) obmc_v += obmc[index + block_w];
  2406. if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
  2407. if(x+block_w>w) obmc_v += obmc[index - block_w];
  2408. //FIXME precalculate this or simplify it somehow else
  2409. d = -dst[index] + (1<<(FRAC_BITS-1));
  2410. dst[index] = d;
  2411. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  2412. aa += obmc_v * obmc_v; //FIXME precalculate this
  2413. }
  2414. }
  2415. }
  2416. *b= backup;
  2417. return av_clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we should not need clipping
  2418. }
  2419. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  2420. const int b_stride = s->b_width << s->block_max_depth;
  2421. const int b_height = s->b_height<< s->block_max_depth;
  2422. int index= x + y*b_stride;
  2423. const BlockNode *b = &s->block[index];
  2424. const BlockNode *left = x ? &s->block[index-1] : &null_block;
  2425. const BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  2426. const BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  2427. const BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  2428. int dmx, dmy;
  2429. // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2430. // int my_context= av_log2(2*FFABS(left->my - top->my));
  2431. if(x<0 || x>=b_stride || y>=b_height)
  2432. return 0;
  2433. /*
  2434. 1 0 0
  2435. 01X 1-2 1
  2436. 001XX 3-6 2-3
  2437. 0001XXX 7-14 4-7
  2438. 00001XXXX 15-30 8-15
  2439. */
  2440. //FIXME try accurate rate
  2441. //FIXME intra and inter predictors if surrounding blocks are not the same type
  2442. if(b->type & BLOCK_INTRA){
  2443. return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  2444. + av_log2(2*FFABS(left->color[1] - b->color[1]))
  2445. + av_log2(2*FFABS(left->color[2] - b->color[2])));
  2446. }else{
  2447. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  2448. dmx-= b->mx;
  2449. dmy-= b->my;
  2450. return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  2451. + av_log2(2*FFABS(dmy))
  2452. + av_log2(2*b->ref));
  2453. }
  2454. }
  2455. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
  2456. Plane *p= &s->plane[plane_index];
  2457. const int block_size = MB_SIZE >> s->block_max_depth;
  2458. const int block_w = plane_index ? block_size/2 : block_size;
  2459. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2460. const int ref_stride= s->current_picture.linesize[plane_index];
  2461. uint8_t *dst= s->current_picture.data[plane_index];
  2462. uint8_t *src= s-> input_picture.data[plane_index];
  2463. IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2464. uint8_t *cur = s->scratchbuf;
  2465. uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];
  2466. const int b_stride = s->b_width << s->block_max_depth;
  2467. const int b_height = s->b_height<< s->block_max_depth;
  2468. const int w= p->width;
  2469. const int h= p->height;
  2470. int distortion;
  2471. int rate= 0;
  2472. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2473. int sx= block_w*mb_x - block_w/2;
  2474. int sy= block_w*mb_y - block_w/2;
  2475. int x0= FFMAX(0,-sx);
  2476. int y0= FFMAX(0,-sy);
  2477. int x1= FFMIN(block_w*2, w-sx);
  2478. int y1= FFMIN(block_w*2, h-sy);
  2479. int i,x,y;
  2480. 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);
  2481. for(y=y0; y<y1; y++){
  2482. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  2483. const IDWTELEM *pred1 = pred + y*obmc_stride;
  2484. uint8_t *cur1 = cur + y*ref_stride;
  2485. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  2486. for(x=x0; x<x1; x++){
  2487. #if FRAC_BITS >= LOG2_OBMC_MAX
  2488. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  2489. #else
  2490. int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);
  2491. #endif
  2492. v = (v + pred1[x]) >> FRAC_BITS;
  2493. if(v&(~255)) v= ~(v>>31);
  2494. dst1[x] = v;
  2495. }
  2496. }
  2497. /* copy the regions where obmc[] = (uint8_t)256 */
  2498. if(LOG2_OBMC_MAX == 8
  2499. && (mb_x == 0 || mb_x == b_stride-1)
  2500. && (mb_y == 0 || mb_y == b_height-1)){
  2501. if(mb_x == 0)
  2502. x1 = block_w;
  2503. else
  2504. x0 = block_w;
  2505. if(mb_y == 0)
  2506. y1 = block_w;
  2507. else
  2508. y0 = block_w;
  2509. for(y=y0; y<y1; y++)
  2510. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  2511. }
  2512. if(block_w==16){
  2513. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  2514. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  2515. /* FIXME cmps overlap but do not cover the wavelet's whole support.
  2516. * So improving the score of one block is not strictly guaranteed
  2517. * to improve the score of the whole frame, thus iterative motion
  2518. * estimation does not always converge. */
  2519. if(s->avctx->me_cmp == FF_CMP_W97)
  2520. distortion = ff_w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2521. else if(s->avctx->me_cmp == FF_CMP_W53)
  2522. distortion = ff_w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2523. else{
  2524. distortion = 0;
  2525. for(i=0; i<4; i++){
  2526. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  2527. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  2528. }
  2529. }
  2530. }else{
  2531. assert(block_w==8);
  2532. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  2533. }
  2534. if(plane_index==0){
  2535. for(i=0; i<4; i++){
  2536. /* ..RRr
  2537. * .RXx.
  2538. * rxx..
  2539. */
  2540. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  2541. }
  2542. if(mb_x == b_stride-2)
  2543. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  2544. }
  2545. return distortion + rate*penalty_factor;
  2546. }
  2547. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2548. int i, y2;
  2549. Plane *p= &s->plane[plane_index];
  2550. const int block_size = MB_SIZE >> s->block_max_depth;
  2551. const int block_w = plane_index ? block_size/2 : block_size;
  2552. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2553. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2554. const int ref_stride= s->current_picture.linesize[plane_index];
  2555. uint8_t *dst= s->current_picture.data[plane_index];
  2556. uint8_t *src= s-> input_picture.data[plane_index];
  2557. //FIXME zero_dst is const but add_yblock changes dst if add is 0 (this is never the case for dst=zero_dst
  2558. // const has only been removed from zero_dst to suppress a warning
  2559. static IDWTELEM zero_dst[4096]; //FIXME
  2560. const int b_stride = s->b_width << s->block_max_depth;
  2561. const int w= p->width;
  2562. const int h= p->height;
  2563. int distortion= 0;
  2564. int rate= 0;
  2565. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2566. for(i=0; i<9; i++){
  2567. int mb_x2= mb_x + (i%3) - 1;
  2568. int mb_y2= mb_y + (i/3) - 1;
  2569. int x= block_w*mb_x2 + block_w/2;
  2570. int y= block_w*mb_y2 + block_w/2;
  2571. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  2572. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  2573. //FIXME find a cleaner/simpler way to skip the outside stuff
  2574. for(y2= y; y2<0; y2++)
  2575. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2576. for(y2= h; y2<y+block_w; y2++)
  2577. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2578. if(x<0){
  2579. for(y2= y; y2<y+block_w; y2++)
  2580. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  2581. }
  2582. if(x+block_w > w){
  2583. for(y2= y; y2<y+block_w; y2++)
  2584. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  2585. }
  2586. assert(block_w== 8 || block_w==16);
  2587. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  2588. }
  2589. if(plane_index==0){
  2590. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  2591. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  2592. /* ..RRRr
  2593. * .RXXx.
  2594. * .RXXx.
  2595. * rxxx.
  2596. */
  2597. if(merged)
  2598. rate = get_block_bits(s, mb_x, mb_y, 2);
  2599. for(i=merged?4:0; i<9; i++){
  2600. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  2601. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  2602. }
  2603. }
  2604. return distortion + rate*penalty_factor;
  2605. }
  2606. static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2607. const int w= b->width;
  2608. const int h= b->height;
  2609. int x, y;
  2610. if(1){
  2611. int run=0;
  2612. int runs[w*h];
  2613. int run_index=0;
  2614. int max_index;
  2615. for(y=0; y<h; y++){
  2616. for(x=0; x<w; x++){
  2617. int v, p=0;
  2618. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2619. v= src[x + y*stride];
  2620. if(y){
  2621. t= src[x + (y-1)*stride];
  2622. if(x){
  2623. lt= src[x - 1 + (y-1)*stride];
  2624. }
  2625. if(x + 1 < w){
  2626. rt= src[x + 1 + (y-1)*stride];
  2627. }
  2628. }
  2629. if(x){
  2630. l= src[x - 1 + y*stride];
  2631. /*if(x > 1){
  2632. if(orientation==1) ll= src[y + (x-2)*stride];
  2633. else ll= src[x - 2 + y*stride];
  2634. }*/
  2635. }
  2636. if(parent){
  2637. int px= x>>1;
  2638. int py= y>>1;
  2639. if(px<b->parent->width && py<b->parent->height)
  2640. p= parent[px + py*2*stride];
  2641. }
  2642. if(!(/*ll|*/l|lt|t|rt|p)){
  2643. if(v){
  2644. runs[run_index++]= run;
  2645. run=0;
  2646. }else{
  2647. run++;
  2648. }
  2649. }
  2650. }
  2651. }
  2652. max_index= run_index;
  2653. runs[run_index++]= run;
  2654. run_index=0;
  2655. run= runs[run_index++];
  2656. put_symbol2(&s->c, b->state[30], max_index, 0);
  2657. if(run_index <= max_index)
  2658. put_symbol2(&s->c, b->state[1], run, 3);
  2659. for(y=0; y<h; y++){
  2660. if(s->c.bytestream_end - s->c.bytestream < w*40){
  2661. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2662. return -1;
  2663. }
  2664. for(x=0; x<w; x++){
  2665. int v, p=0;
  2666. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  2667. v= src[x + y*stride];
  2668. if(y){
  2669. t= src[x + (y-1)*stride];
  2670. if(x){
  2671. lt= src[x - 1 + (y-1)*stride];
  2672. }
  2673. if(x + 1 < w){
  2674. rt= src[x + 1 + (y-1)*stride];
  2675. }
  2676. }
  2677. if(x){
  2678. l= src[x - 1 + y*stride];
  2679. /*if(x > 1){
  2680. if(orientation==1) ll= src[y + (x-2)*stride];
  2681. else ll= src[x - 2 + y*stride];
  2682. }*/
  2683. }
  2684. if(parent){
  2685. int px= x>>1;
  2686. int py= y>>1;
  2687. if(px<b->parent->width && py<b->parent->height)
  2688. p= parent[px + py*2*stride];
  2689. }
  2690. if(/*ll|*/l|lt|t|rt|p){
  2691. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2692. put_rac(&s->c, &b->state[0][context], !!v);
  2693. }else{
  2694. if(!run){
  2695. run= runs[run_index++];
  2696. if(run_index <= max_index)
  2697. put_symbol2(&s->c, b->state[1], run, 3);
  2698. assert(v);
  2699. }else{
  2700. run--;
  2701. assert(!v);
  2702. }
  2703. }
  2704. if(v){
  2705. int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
  2706. int l2= 2*FFABS(l) + (l<0);
  2707. int t2= 2*FFABS(t) + (t<0);
  2708. put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
  2709. put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
  2710. }
  2711. }
  2712. }
  2713. }
  2714. return 0;
  2715. }
  2716. static int encode_subband(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
  2717. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  2718. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  2719. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  2720. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  2721. }
  2722. 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){
  2723. const int b_stride= s->b_width << s->block_max_depth;
  2724. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2725. BlockNode backup= *block;
  2726. int rd, index, value;
  2727. assert(mb_x>=0 && mb_y>=0);
  2728. assert(mb_x<b_stride);
  2729. if(intra){
  2730. block->color[0] = p[0];
  2731. block->color[1] = p[1];
  2732. block->color[2] = p[2];
  2733. block->type |= BLOCK_INTRA;
  2734. }else{
  2735. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  2736. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  2737. if(s->me_cache[index] == value)
  2738. return 0;
  2739. s->me_cache[index]= value;
  2740. block->mx= p[0];
  2741. block->my= p[1];
  2742. block->type &= ~BLOCK_INTRA;
  2743. }
  2744. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  2745. //FIXME chroma
  2746. if(rd < *best_rd){
  2747. *best_rd= rd;
  2748. return 1;
  2749. }else{
  2750. *block= backup;
  2751. return 0;
  2752. }
  2753. }
  2754. /* special case for int[2] args we discard afterwards,
  2755. * fixes compilation problem with gcc 2.95 */
  2756. 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){
  2757. int p[2] = {p0, p1};
  2758. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  2759. }
  2760. 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){
  2761. const int b_stride= s->b_width << s->block_max_depth;
  2762. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2763. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  2764. int rd, index, value;
  2765. assert(mb_x>=0 && mb_y>=0);
  2766. assert(mb_x<b_stride);
  2767. assert(((mb_x|mb_y)&1) == 0);
  2768. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  2769. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  2770. if(s->me_cache[index] == value)
  2771. return 0;
  2772. s->me_cache[index]= value;
  2773. block->mx= p0;
  2774. block->my= p1;
  2775. block->ref= ref;
  2776. block->type &= ~BLOCK_INTRA;
  2777. block[1]= block[b_stride]= block[b_stride+1]= *block;
  2778. rd= get_4block_rd(s, mb_x, mb_y, 0);
  2779. //FIXME chroma
  2780. if(rd < *best_rd){
  2781. *best_rd= rd;
  2782. return 1;
  2783. }else{
  2784. block[0]= backup[0];
  2785. block[1]= backup[1];
  2786. block[b_stride]= backup[2];
  2787. block[b_stride+1]= backup[3];
  2788. return 0;
  2789. }
  2790. }
  2791. static void iterative_me(SnowContext *s){
  2792. int pass, mb_x, mb_y;
  2793. const int b_width = s->b_width << s->block_max_depth;
  2794. const int b_height= s->b_height << s->block_max_depth;
  2795. const int b_stride= b_width;
  2796. int color[3];
  2797. {
  2798. RangeCoder r = s->c;
  2799. uint8_t state[sizeof(s->block_state)];
  2800. memcpy(state, s->block_state, sizeof(s->block_state));
  2801. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  2802. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  2803. encode_q_branch(s, 0, mb_x, mb_y);
  2804. s->c = r;
  2805. memcpy(s->block_state, state, sizeof(s->block_state));
  2806. }
  2807. for(pass=0; pass<25; pass++){
  2808. int change= 0;
  2809. for(mb_y= 0; mb_y<b_height; mb_y++){
  2810. for(mb_x= 0; mb_x<b_width; mb_x++){
  2811. int dia_change, i, j, ref;
  2812. int best_rd= INT_MAX, ref_rd;
  2813. BlockNode backup, ref_b;
  2814. const int index= mb_x + mb_y * b_stride;
  2815. BlockNode *block= &s->block[index];
  2816. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  2817. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  2818. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  2819. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  2820. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  2821. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  2822. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  2823. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  2824. const int b_w= (MB_SIZE >> s->block_max_depth);
  2825. uint8_t obmc_edged[b_w*2][b_w*2];
  2826. if(pass && (block->type & BLOCK_OPT))
  2827. continue;
  2828. block->type |= BLOCK_OPT;
  2829. backup= *block;
  2830. if(!s->me_cache_generation)
  2831. memset(s->me_cache, 0, sizeof(s->me_cache));
  2832. s->me_cache_generation += 1<<22;
  2833. //FIXME precalculate
  2834. {
  2835. int x, y;
  2836. memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
  2837. if(mb_x==0)
  2838. for(y=0; y<b_w*2; y++)
  2839. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  2840. if(mb_x==b_stride-1)
  2841. for(y=0; y<b_w*2; y++)
  2842. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  2843. if(mb_y==0){
  2844. for(x=0; x<b_w*2; x++)
  2845. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  2846. for(y=1; y<b_w; y++)
  2847. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  2848. }
  2849. if(mb_y==b_height-1){
  2850. for(x=0; x<b_w*2; x++)
  2851. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  2852. for(y=b_w; y<b_w*2-1; y++)
  2853. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  2854. }
  2855. }
  2856. //skip stuff outside the picture
  2857. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1){
  2858. uint8_t *src= s-> input_picture.data[0];
  2859. uint8_t *dst= s->current_picture.data[0];
  2860. const int stride= s->current_picture.linesize[0];
  2861. const int block_w= MB_SIZE >> s->block_max_depth;
  2862. const int sx= block_w*mb_x - block_w/2;
  2863. const int sy= block_w*mb_y - block_w/2;
  2864. const int w= s->plane[0].width;
  2865. const int h= s->plane[0].height;
  2866. int y;
  2867. for(y=sy; y<0; y++)
  2868. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2869. for(y=h; y<sy+block_w*2; y++)
  2870. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2871. if(sx<0){
  2872. for(y=sy; y<sy+block_w*2; y++)
  2873. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  2874. }
  2875. if(sx+block_w*2 > w){
  2876. for(y=sy; y<sy+block_w*2; y++)
  2877. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  2878. }
  2879. }
  2880. // intra(black) = neighbors' contribution to the current block
  2881. for(i=0; i<3; i++)
  2882. color[i]= get_dc(s, mb_x, mb_y, i);
  2883. // get previous score (cannot be cached due to OBMC)
  2884. if(pass > 0 && (block->type&BLOCK_INTRA)){
  2885. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  2886. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  2887. }else
  2888. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  2889. ref_b= *block;
  2890. ref_rd= best_rd;
  2891. for(ref=0; ref < s->ref_frames; ref++){
  2892. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  2893. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  2894. continue;
  2895. block->ref= ref;
  2896. best_rd= INT_MAX;
  2897. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  2898. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  2899. if(tb)
  2900. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  2901. if(lb)
  2902. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  2903. if(rb)
  2904. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  2905. if(bb)
  2906. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  2907. /* fullpel ME */
  2908. //FIXME avoid subpel interpolation / round to nearest integer
  2909. do{
  2910. dia_change=0;
  2911. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  2912. for(j=0; j<i; j++){
  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. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2917. }
  2918. }
  2919. }while(dia_change);
  2920. /* subpel ME */
  2921. do{
  2922. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  2923. dia_change=0;
  2924. for(i=0; i<8; i++)
  2925. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  2926. }while(dia_change);
  2927. //FIXME or try the standard 2 pass qpel or similar
  2928. mvr[0][0]= block->mx;
  2929. mvr[0][1]= block->my;
  2930. if(ref_rd > best_rd){
  2931. ref_rd= best_rd;
  2932. ref_b= *block;
  2933. }
  2934. }
  2935. best_rd= ref_rd;
  2936. *block= ref_b;
  2937. #if 1
  2938. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  2939. //FIXME RD style color selection
  2940. #endif
  2941. if(!same_block(block, &backup)){
  2942. if(tb ) tb ->type &= ~BLOCK_OPT;
  2943. if(lb ) lb ->type &= ~BLOCK_OPT;
  2944. if(rb ) rb ->type &= ~BLOCK_OPT;
  2945. if(bb ) bb ->type &= ~BLOCK_OPT;
  2946. if(tlb) tlb->type &= ~BLOCK_OPT;
  2947. if(trb) trb->type &= ~BLOCK_OPT;
  2948. if(blb) blb->type &= ~BLOCK_OPT;
  2949. if(brb) brb->type &= ~BLOCK_OPT;
  2950. change ++;
  2951. }
  2952. }
  2953. }
  2954. av_log(s->avctx, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  2955. if(!change)
  2956. break;
  2957. }
  2958. if(s->block_max_depth == 1){
  2959. int change= 0;
  2960. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  2961. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  2962. int i;
  2963. int best_rd, init_rd;
  2964. const int index= mb_x + mb_y * b_stride;
  2965. BlockNode *b[4];
  2966. b[0]= &s->block[index];
  2967. b[1]= b[0]+1;
  2968. b[2]= b[0]+b_stride;
  2969. b[3]= b[2]+1;
  2970. if(same_block(b[0], b[1]) &&
  2971. same_block(b[0], b[2]) &&
  2972. same_block(b[0], b[3]))
  2973. continue;
  2974. if(!s->me_cache_generation)
  2975. memset(s->me_cache, 0, sizeof(s->me_cache));
  2976. s->me_cache_generation += 1<<22;
  2977. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  2978. //FIXME more multiref search?
  2979. check_4block_inter(s, mb_x, mb_y,
  2980. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  2981. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  2982. for(i=0; i<4; i++)
  2983. if(!(b[i]->type&BLOCK_INTRA))
  2984. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  2985. if(init_rd != best_rd)
  2986. change++;
  2987. }
  2988. }
  2989. av_log(s->avctx, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  2990. }
  2991. }
  2992. static void encode_blocks(SnowContext *s, int search){
  2993. int x, y;
  2994. int w= s->b_width;
  2995. int h= s->b_height;
  2996. if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
  2997. iterative_me(s);
  2998. for(y=0; y<h; y++){
  2999. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  3000. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  3001. return;
  3002. }
  3003. for(x=0; x<w; x++){
  3004. if(s->avctx->me_method == ME_ITER || !search)
  3005. encode_q_branch2(s, 0, x, y);
  3006. else
  3007. encode_q_branch (s, 0, x, y);
  3008. }
  3009. }
  3010. }
  3011. static void quantize(SnowContext *s, SubBand *b, IDWTELEM *dst, DWTELEM *src, int stride, int bias){
  3012. const int w= b->width;
  3013. const int h= b->height;
  3014. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  3015. const int qmul= qexp[qlog&(QROOT-1)]<<((qlog>>QSHIFT) + ENCODER_EXTRA_BITS);
  3016. int x,y, thres1, thres2;
  3017. if(s->qlog == LOSSLESS_QLOG){
  3018. for(y=0; y<h; y++)
  3019. for(x=0; x<w; x++)
  3020. dst[x + y*stride]= src[x + y*stride];
  3021. return;
  3022. }
  3023. bias= bias ? 0 : (3*qmul)>>3;
  3024. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  3025. thres2= 2*thres1;
  3026. if(!bias){
  3027. for(y=0; y<h; y++){
  3028. for(x=0; x<w; x++){
  3029. int i= src[x + y*stride];
  3030. if((unsigned)(i+thres1) > thres2){
  3031. if(i>=0){
  3032. i<<= QEXPSHIFT;
  3033. i/= qmul; //FIXME optimize
  3034. dst[x + y*stride]= i;
  3035. }else{
  3036. i= -i;
  3037. i<<= QEXPSHIFT;
  3038. i/= qmul; //FIXME optimize
  3039. dst[x + y*stride]= -i;
  3040. }
  3041. }else
  3042. dst[x + y*stride]= 0;
  3043. }
  3044. }
  3045. }else{
  3046. for(y=0; y<h; y++){
  3047. for(x=0; x<w; x++){
  3048. int i= src[x + y*stride];
  3049. if((unsigned)(i+thres1) > thres2){
  3050. if(i>=0){
  3051. i<<= QEXPSHIFT;
  3052. i= (i + bias) / qmul; //FIXME optimize
  3053. dst[x + y*stride]= i;
  3054. }else{
  3055. i= -i;
  3056. i<<= QEXPSHIFT;
  3057. i= (i + bias) / qmul; //FIXME optimize
  3058. dst[x + y*stride]= -i;
  3059. }
  3060. }else
  3061. dst[x + y*stride]= 0;
  3062. }
  3063. }
  3064. }
  3065. }
  3066. static void dequantize(SnowContext *s, SubBand *b, IDWTELEM *src, int stride){
  3067. const int w= b->width;
  3068. const int h= b->height;
  3069. const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
  3070. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3071. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  3072. int x,y;
  3073. if(s->qlog == LOSSLESS_QLOG) return;
  3074. for(y=0; y<h; y++){
  3075. for(x=0; x<w; x++){
  3076. int i= src[x + y*stride];
  3077. if(i<0){
  3078. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  3079. }else if(i>0){
  3080. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  3081. }
  3082. }
  3083. }
  3084. }
  3085. static void decorrelate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  3086. const int w= b->width;
  3087. const int h= b->height;
  3088. int x,y;
  3089. for(y=h-1; y>=0; y--){
  3090. for(x=w-1; x>=0; x--){
  3091. int i= x + y*stride;
  3092. if(x){
  3093. if(use_median){
  3094. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3095. else src[i] -= src[i - 1];
  3096. }else{
  3097. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3098. else src[i] -= src[i - 1];
  3099. }
  3100. }else{
  3101. if(y) src[i] -= src[i - stride];
  3102. }
  3103. }
  3104. }
  3105. }
  3106. static void correlate(SnowContext *s, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median){
  3107. const int w= b->width;
  3108. const int h= b->height;
  3109. int x,y;
  3110. for(y=0; y<h; y++){
  3111. for(x=0; x<w; x++){
  3112. int i= x + y*stride;
  3113. if(x){
  3114. if(use_median){
  3115. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3116. else src[i] += src[i - 1];
  3117. }else{
  3118. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3119. else src[i] += src[i - 1];
  3120. }
  3121. }else{
  3122. if(y) src[i] += src[i - stride];
  3123. }
  3124. }
  3125. }
  3126. }
  3127. static void encode_qlogs(SnowContext *s){
  3128. int plane_index, level, orientation;
  3129. for(plane_index=0; plane_index<2; plane_index++){
  3130. for(level=0; level<s->spatial_decomposition_count; level++){
  3131. for(orientation=level ? 1:0; orientation<4; orientation++){
  3132. if(orientation==2) continue;
  3133. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  3134. }
  3135. }
  3136. }
  3137. }
  3138. static void encode_header(SnowContext *s){
  3139. int plane_index, i;
  3140. uint8_t kstate[32];
  3141. memset(kstate, MID_STATE, sizeof(kstate));
  3142. put_rac(&s->c, kstate, s->keyframe);
  3143. if(s->keyframe || s->always_reset){
  3144. reset_contexts(s);
  3145. s->last_spatial_decomposition_type=
  3146. s->last_qlog=
  3147. s->last_qbias=
  3148. s->last_mv_scale=
  3149. s->last_block_max_depth= 0;
  3150. for(plane_index=0; plane_index<2; plane_index++){
  3151. Plane *p= &s->plane[plane_index];
  3152. p->last_htaps=0;
  3153. p->last_diag_mc=0;
  3154. memset(p->last_hcoeff, 0, sizeof(p->last_hcoeff));
  3155. }
  3156. }
  3157. if(s->keyframe){
  3158. put_symbol(&s->c, s->header_state, s->version, 0);
  3159. put_rac(&s->c, s->header_state, s->always_reset);
  3160. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  3161. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  3162. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3163. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  3164. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  3165. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  3166. put_rac(&s->c, s->header_state, s->spatial_scalability);
  3167. // put_rac(&s->c, s->header_state, s->rate_scalability);
  3168. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  3169. encode_qlogs(s);
  3170. }
  3171. if(!s->keyframe){
  3172. int update_mc=0;
  3173. for(plane_index=0; plane_index<2; plane_index++){
  3174. Plane *p= &s->plane[plane_index];
  3175. update_mc |= p->last_htaps != p->htaps;
  3176. update_mc |= p->last_diag_mc != p->diag_mc;
  3177. update_mc |= !!memcmp(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  3178. }
  3179. put_rac(&s->c, s->header_state, update_mc);
  3180. if(update_mc){
  3181. for(plane_index=0; plane_index<2; plane_index++){
  3182. Plane *p= &s->plane[plane_index];
  3183. put_rac(&s->c, s->header_state, p->diag_mc);
  3184. put_symbol(&s->c, s->header_state, p->htaps/2-1, 0);
  3185. for(i= p->htaps/2; i; i--)
  3186. put_symbol(&s->c, s->header_state, FFABS(p->hcoeff[i]), 0);
  3187. }
  3188. }
  3189. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3190. put_rac(&s->c, s->header_state, 1);
  3191. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3192. encode_qlogs(s);
  3193. }else
  3194. put_rac(&s->c, s->header_state, 0);
  3195. }
  3196. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  3197. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  3198. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  3199. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  3200. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  3201. }
  3202. static void update_last_header_values(SnowContext *s){
  3203. int plane_index;
  3204. if(!s->keyframe){
  3205. for(plane_index=0; plane_index<2; plane_index++){
  3206. Plane *p= &s->plane[plane_index];
  3207. p->last_diag_mc= p->diag_mc;
  3208. p->last_htaps = p->htaps;
  3209. memcpy(p->last_hcoeff, p->hcoeff, sizeof(p->hcoeff));
  3210. }
  3211. }
  3212. s->last_spatial_decomposition_type = s->spatial_decomposition_type;
  3213. s->last_qlog = s->qlog;
  3214. s->last_qbias = s->qbias;
  3215. s->last_mv_scale = s->mv_scale;
  3216. s->last_block_max_depth = s->block_max_depth;
  3217. s->last_spatial_decomposition_count = s->spatial_decomposition_count;
  3218. }
  3219. static int qscale2qlog(int qscale){
  3220. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  3221. + 61*QROOT/8; //<64 >60
  3222. }
  3223. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  3224. {
  3225. /* Estimate the frame's complexity as a sum of weighted dwt coefficients.
  3226. * FIXME we know exact mv bits at this point,
  3227. * but ratecontrol isn't set up to include them. */
  3228. uint32_t coef_sum= 0;
  3229. int level, orientation, delta_qlog;
  3230. for(level=0; level<s->spatial_decomposition_count; level++){
  3231. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3232. SubBand *b= &s->plane[0].band[level][orientation];
  3233. IDWTELEM *buf= b->ibuf;
  3234. const int w= b->width;
  3235. const int h= b->height;
  3236. const int stride= b->stride;
  3237. const int qlog= av_clip(2*QROOT + b->qlog, 0, QROOT*16);
  3238. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3239. const int qdiv= (1<<16)/qmul;
  3240. int x, y;
  3241. //FIXME this is ugly
  3242. for(y=0; y<h; y++)
  3243. for(x=0; x<w; x++)
  3244. buf[x+y*stride]= b->buf[x+y*stride];
  3245. if(orientation==0)
  3246. decorrelate(s, b, buf, stride, 1, 0);
  3247. for(y=0; y<h; y++)
  3248. for(x=0; x<w; x++)
  3249. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  3250. }
  3251. }
  3252. /* ugly, ratecontrol just takes a sqrt again */
  3253. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  3254. assert(coef_sum < INT_MAX);
  3255. if(pict->pict_type == FF_I_TYPE){
  3256. s->m.current_picture.mb_var_sum= coef_sum;
  3257. s->m.current_picture.mc_mb_var_sum= 0;
  3258. }else{
  3259. s->m.current_picture.mc_mb_var_sum= coef_sum;
  3260. s->m.current_picture.mb_var_sum= 0;
  3261. }
  3262. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  3263. if (pict->quality < 0)
  3264. return INT_MIN;
  3265. s->lambda= pict->quality * 3/2;
  3266. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  3267. s->qlog+= delta_qlog;
  3268. return delta_qlog;
  3269. }
  3270. static void calculate_visual_weight(SnowContext *s, Plane *p){
  3271. int width = p->width;
  3272. int height= p->height;
  3273. int level, orientation, x, y;
  3274. for(level=0; level<s->spatial_decomposition_count; level++){
  3275. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3276. SubBand *b= &p->band[level][orientation];
  3277. IDWTELEM *ibuf= b->ibuf;
  3278. int64_t error=0;
  3279. memset(s->spatial_idwt_buffer, 0, sizeof(*s->spatial_idwt_buffer)*width*height);
  3280. ibuf[b->width/2 + b->height/2*b->stride]= 256*16;
  3281. ff_spatial_idwt(s->spatial_idwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3282. for(y=0; y<height; y++){
  3283. for(x=0; x<width; x++){
  3284. int64_t d= s->spatial_idwt_buffer[x + y*width]*16;
  3285. error += d*d;
  3286. }
  3287. }
  3288. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  3289. }
  3290. }
  3291. }
  3292. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  3293. SnowContext *s = avctx->priv_data;
  3294. RangeCoder * const c= &s->c;
  3295. AVFrame *pict = data;
  3296. const int width= s->avctx->width;
  3297. const int height= s->avctx->height;
  3298. int level, orientation, plane_index, i, y;
  3299. uint8_t rc_header_bak[sizeof(s->header_state)];
  3300. uint8_t rc_block_bak[sizeof(s->block_state)];
  3301. ff_init_range_encoder(c, buf, buf_size);
  3302. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3303. for(i=0; i<3; i++){
  3304. int shift= !!i;
  3305. for(y=0; y<(height>>shift); y++)
  3306. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  3307. &pict->data[i][y * pict->linesize[i]],
  3308. width>>shift);
  3309. }
  3310. s->new_picture = *pict;
  3311. s->m.picture_number= avctx->frame_number;
  3312. if(avctx->flags&CODEC_FLAG_PASS2){
  3313. s->m.pict_type =
  3314. pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  3315. s->keyframe= pict->pict_type==FF_I_TYPE;
  3316. if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
  3317. pict->quality= ff_rate_estimate_qscale(&s->m, 0);
  3318. if (pict->quality < 0)
  3319. return -1;
  3320. }
  3321. }else{
  3322. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  3323. s->m.pict_type=
  3324. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  3325. }
  3326. if(s->pass1_rc && avctx->frame_number == 0)
  3327. pict->quality= 2*FF_QP2LAMBDA;
  3328. if(pict->quality){
  3329. s->qlog= qscale2qlog(pict->quality);
  3330. s->lambda = pict->quality * 3/2;
  3331. }
  3332. if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
  3333. s->qlog= LOSSLESS_QLOG;
  3334. s->lambda = 0;
  3335. }//else keep previous frame's qlog until after motion estimation
  3336. frame_start(s);
  3337. s->m.current_picture_ptr= &s->m.current_picture;
  3338. s->m.last_picture.pts= s->m.current_picture.pts;
  3339. s->m.current_picture.pts= pict->pts;
  3340. if(pict->pict_type == FF_P_TYPE){
  3341. int block_width = (width +15)>>4;
  3342. int block_height= (height+15)>>4;
  3343. int stride= s->current_picture.linesize[0];
  3344. assert(s->current_picture.data[0]);
  3345. assert(s->last_picture[0].data[0]);
  3346. s->m.avctx= s->avctx;
  3347. s->m.current_picture.data[0]= s->current_picture.data[0];
  3348. s->m. last_picture.data[0]= s->last_picture[0].data[0];
  3349. s->m. new_picture.data[0]= s-> input_picture.data[0];
  3350. s->m. last_picture_ptr= &s->m. last_picture;
  3351. s->m.linesize=
  3352. s->m. last_picture.linesize[0]=
  3353. s->m. new_picture.linesize[0]=
  3354. s->m.current_picture.linesize[0]= stride;
  3355. s->m.uvlinesize= s->current_picture.linesize[1];
  3356. s->m.width = width;
  3357. s->m.height= height;
  3358. s->m.mb_width = block_width;
  3359. s->m.mb_height= block_height;
  3360. s->m.mb_stride= s->m.mb_width+1;
  3361. s->m.b8_stride= 2*s->m.mb_width+1;
  3362. s->m.f_code=1;
  3363. s->m.pict_type= pict->pict_type;
  3364. s->m.me_method= s->avctx->me_method;
  3365. s->m.me.scene_change_score=0;
  3366. s->m.flags= s->avctx->flags;
  3367. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  3368. s->m.out_format= FMT_H263;
  3369. s->m.unrestricted_mv= 1;
  3370. s->m.lambda = s->lambda;
  3371. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  3372. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  3373. s->m.dsp= s->dsp; //move
  3374. ff_init_me(&s->m);
  3375. s->dsp= s->m.dsp;
  3376. }
  3377. if(s->pass1_rc){
  3378. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  3379. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  3380. }
  3381. redo_frame:
  3382. if(pict->pict_type == FF_I_TYPE)
  3383. s->spatial_decomposition_count= 5;
  3384. else
  3385. s->spatial_decomposition_count= 5;
  3386. s->m.pict_type = pict->pict_type;
  3387. s->qbias= pict->pict_type == FF_P_TYPE ? 2 : 0;
  3388. common_init_after_header(avctx);
  3389. if(s->last_spatial_decomposition_count != s->spatial_decomposition_count){
  3390. for(plane_index=0; plane_index<3; plane_index++){
  3391. calculate_visual_weight(s, &s->plane[plane_index]);
  3392. }
  3393. }
  3394. encode_header(s);
  3395. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3396. encode_blocks(s, 1);
  3397. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  3398. for(plane_index=0; plane_index<3; plane_index++){
  3399. Plane *p= &s->plane[plane_index];
  3400. int w= p->width;
  3401. int h= p->height;
  3402. int x, y;
  3403. // int bits= put_bits_count(&s->c.pb);
  3404. if(!(avctx->flags2 & CODEC_FLAG2_MEMC_ONLY)){
  3405. //FIXME optimize
  3406. if(pict->data[plane_index]) //FIXME gray hack
  3407. for(y=0; y<h; y++){
  3408. for(x=0; x<w; x++){
  3409. s->spatial_idwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3410. }
  3411. }
  3412. predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
  3413. if( plane_index==0
  3414. && pict->pict_type == FF_P_TYPE
  3415. && !(avctx->flags&CODEC_FLAG_PASS2)
  3416. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3417. ff_init_range_encoder(c, buf, buf_size);
  3418. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3419. pict->pict_type= FF_I_TYPE;
  3420. s->keyframe=1;
  3421. s->current_picture.key_frame=1;
  3422. goto redo_frame;
  3423. }
  3424. if(s->qlog == LOSSLESS_QLOG){
  3425. for(y=0; y<h; y++){
  3426. for(x=0; x<w; x++){
  3427. s->spatial_dwt_buffer[y*w + x]= (s->spatial_idwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  3428. }
  3429. }
  3430. }else{
  3431. for(y=0; y<h; y++){
  3432. for(x=0; x<w; x++){
  3433. s->spatial_dwt_buffer[y*w + x]=s->spatial_idwt_buffer[y*w + x]<<ENCODER_EXTRA_BITS;
  3434. }
  3435. }
  3436. }
  3437. /* if(QUANTIZE2)
  3438. dwt_quantize(s, p, s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type);
  3439. else*/
  3440. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3441. if(s->pass1_rc && plane_index==0){
  3442. int delta_qlog = ratecontrol_1pass(s, pict);
  3443. if (delta_qlog <= INT_MIN)
  3444. return -1;
  3445. if(delta_qlog){
  3446. //reordering qlog in the bitstream would eliminate this reset
  3447. ff_init_range_encoder(c, buf, buf_size);
  3448. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  3449. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  3450. encode_header(s);
  3451. encode_blocks(s, 0);
  3452. }
  3453. }
  3454. for(level=0; level<s->spatial_decomposition_count; level++){
  3455. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3456. SubBand *b= &p->band[level][orientation];
  3457. if(!QUANTIZE2)
  3458. quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
  3459. if(orientation==0)
  3460. decorrelate(s, b, b->ibuf, b->stride, pict->pict_type == FF_P_TYPE, 0);
  3461. encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
  3462. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3463. if(orientation==0)
  3464. correlate(s, b, b->ibuf, b->stride, 1, 0);
  3465. }
  3466. }
  3467. for(level=0; level<s->spatial_decomposition_count; level++){
  3468. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3469. SubBand *b= &p->band[level][orientation];
  3470. dequantize(s, b, b->ibuf, b->stride);
  3471. }
  3472. }
  3473. ff_spatial_idwt(s->spatial_idwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3474. if(s->qlog == LOSSLESS_QLOG){
  3475. for(y=0; y<h; y++){
  3476. for(x=0; x<w; x++){
  3477. s->spatial_idwt_buffer[y*w + x]<<=FRAC_BITS;
  3478. }
  3479. }
  3480. }
  3481. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3482. }else{
  3483. //ME/MC only
  3484. if(pict->pict_type == FF_I_TYPE){
  3485. for(y=0; y<h; y++){
  3486. for(x=0; x<w; x++){
  3487. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  3488. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3489. }
  3490. }
  3491. }else{
  3492. memset(s->spatial_idwt_buffer, 0, sizeof(IDWTELEM)*w*h);
  3493. predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
  3494. }
  3495. }
  3496. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3497. int64_t error= 0;
  3498. if(pict->data[plane_index]) //FIXME gray hack
  3499. for(y=0; y<h; y++){
  3500. for(x=0; x<w; x++){
  3501. 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];
  3502. error += d*d;
  3503. }
  3504. }
  3505. s->avctx->error[plane_index] += error;
  3506. s->current_picture.error[plane_index] = error;
  3507. }
  3508. }
  3509. update_last_header_values(s);
  3510. release_buffer(avctx);
  3511. s->current_picture.coded_picture_number = avctx->frame_number;
  3512. s->current_picture.pict_type = pict->pict_type;
  3513. s->current_picture.quality = pict->quality;
  3514. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3515. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  3516. s->m.current_picture.display_picture_number =
  3517. s->m.current_picture.coded_picture_number = avctx->frame_number;
  3518. s->m.current_picture.quality = pict->quality;
  3519. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  3520. if(s->pass1_rc)
  3521. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  3522. return -1;
  3523. if(avctx->flags&CODEC_FLAG_PASS1)
  3524. ff_write_pass1_stats(&s->m);
  3525. s->m.last_pict_type = s->m.pict_type;
  3526. avctx->frame_bits = s->m.frame_bits;
  3527. avctx->mv_bits = s->m.mv_bits;
  3528. avctx->misc_bits = s->m.misc_bits;
  3529. avctx->p_tex_bits = s->m.p_tex_bits;
  3530. emms_c();
  3531. return ff_rac_terminate(c);
  3532. }
  3533. static av_cold int encode_end(AVCodecContext *avctx)
  3534. {
  3535. SnowContext *s = avctx->priv_data;
  3536. common_end(s);
  3537. if (s->input_picture.data[0])
  3538. avctx->release_buffer(avctx, &s->input_picture);
  3539. av_free(avctx->stats_out);
  3540. return 0;
  3541. }
  3542. AVCodec snow_encoder = {
  3543. "snow",
  3544. CODEC_TYPE_VIDEO,
  3545. CODEC_ID_SNOW,
  3546. sizeof(SnowContext),
  3547. encode_init,
  3548. encode_frame,
  3549. encode_end,
  3550. .long_name = NULL_IF_CONFIG_SMALL("Snow"),
  3551. };
  3552. #endif
  3553. #ifdef TEST
  3554. #undef malloc
  3555. #undef free
  3556. #undef printf
  3557. #include "libavutil/lfg.h"
  3558. int main(void){
  3559. int width=256;
  3560. int height=256;
  3561. int buffer[2][width*height];
  3562. SnowContext s;
  3563. int i;
  3564. AVLFG prng;
  3565. s.spatial_decomposition_count=6;
  3566. s.spatial_decomposition_type=1;
  3567. av_lfg_init(&prng, 1);
  3568. printf("testing 5/3 DWT\n");
  3569. for(i=0; i<width*height; i++)
  3570. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  3571. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3572. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3573. for(i=0; i<width*height; i++)
  3574. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  3575. printf("testing 9/7 DWT\n");
  3576. s.spatial_decomposition_type=0;
  3577. for(i=0; i<width*height; i++)
  3578. buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
  3579. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3580. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3581. for(i=0; i<width*height; i++)
  3582. if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
  3583. #if 0
  3584. printf("testing AC coder\n");
  3585. memset(s.header_state, 0, sizeof(s.header_state));
  3586. ff_init_range_encoder(&s.c, buffer[0], 256*256);
  3587. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3588. for(i=-256; i<256; i++){
  3589. put_symbol(&s.c, s.header_state, i*i*i/3*FFABS(i), 1);
  3590. }
  3591. ff_rac_terminate(&s.c);
  3592. memset(s.header_state, 0, sizeof(s.header_state));
  3593. ff_init_range_decoder(&s.c, buffer[0], 256*256);
  3594. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3595. for(i=-256; i<256; i++){
  3596. int j;
  3597. j= get_symbol(&s.c, s.header_state, 1);
  3598. if(j!=i*i*i/3*FFABS(i)) printf("fsck: %d != %d\n", i, j);
  3599. }
  3600. #endif
  3601. {
  3602. int level, orientation, x, y;
  3603. int64_t errors[8][4];
  3604. int64_t g=0;
  3605. memset(errors, 0, sizeof(errors));
  3606. s.spatial_decomposition_count=3;
  3607. s.spatial_decomposition_type=0;
  3608. for(level=0; level<s.spatial_decomposition_count; level++){
  3609. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3610. int w= width >> (s.spatial_decomposition_count-level);
  3611. int h= height >> (s.spatial_decomposition_count-level);
  3612. int stride= width << (s.spatial_decomposition_count-level);
  3613. DWTELEM *buf= buffer[0];
  3614. int64_t error=0;
  3615. if(orientation&1) buf+=w;
  3616. if(orientation>1) buf+=stride>>1;
  3617. memset(buffer[0], 0, sizeof(int)*width*height);
  3618. buf[w/2 + h/2*stride]= 256*256;
  3619. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3620. for(y=0; y<height; y++){
  3621. for(x=0; x<width; x++){
  3622. int64_t d= buffer[0][x + y*width];
  3623. error += d*d;
  3624. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
  3625. }
  3626. if(FFABS(height/2-y)<9 && level==2) printf("\n");
  3627. }
  3628. error= (int)(sqrt(error)+0.5);
  3629. errors[level][orientation]= error;
  3630. if(g) g=av_gcd(g, error);
  3631. else g= error;
  3632. }
  3633. }
  3634. printf("static int const visual_weight[][4]={\n");
  3635. for(level=0; level<s.spatial_decomposition_count; level++){
  3636. printf(" {");
  3637. for(orientation=0; orientation<4; orientation++){
  3638. printf("%8"PRId64",", errors[level][orientation]/g);
  3639. }
  3640. printf("},\n");
  3641. }
  3642. printf("};\n");
  3643. {
  3644. int level=2;
  3645. int w= width >> (s.spatial_decomposition_count-level);
  3646. //int h= height >> (s.spatial_decomposition_count-level);
  3647. int stride= width << (s.spatial_decomposition_count-level);
  3648. DWTELEM *buf= buffer[0];
  3649. int64_t error=0;
  3650. buf+=w;
  3651. buf+=stride>>1;
  3652. memset(buffer[0], 0, sizeof(int)*width*height);
  3653. #if 1
  3654. for(y=0; y<height; y++){
  3655. for(x=0; x<width; x++){
  3656. int tab[4]={0,2,3,1};
  3657. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  3658. }
  3659. }
  3660. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3661. #else
  3662. for(y=0; y<h; y++){
  3663. for(x=0; x<w; x++){
  3664. buf[x + y*stride ]=169;
  3665. buf[x + y*stride-w]=64;
  3666. }
  3667. }
  3668. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  3669. #endif
  3670. for(y=0; y<height; y++){
  3671. for(x=0; x<width; x++){
  3672. int64_t d= buffer[0][x + y*width];
  3673. error += d*d;
  3674. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
  3675. }
  3676. if(FFABS(height/2-y)<9) printf("\n");
  3677. }
  3678. }
  3679. }
  3680. return 0;
  3681. }
  3682. #endif /* TEST */