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.

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