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.

4683 lines
166KB

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