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.

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