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.

4869 lines
181KB

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