You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4826 lines
180KB

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