You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4851 lines
180KB

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