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.

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