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.

4586 lines
166KB

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