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.

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