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.

4928 lines
181KB

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