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.

4826 lines
179KB

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