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.

4469 lines
162KB

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