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.

4578 lines
166KB

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