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.

4832 lines
180KB

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