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.

4902 lines
180KB

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