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.

4511 lines
163KB

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