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.

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