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.

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