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.

4768 lines
175KB

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