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.

4707 lines
168KB

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