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.

4709 lines
167KB

  1. /*
  2. * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avcodec.h"
  21. #include "common.h"
  22. #include "dsputil.h"
  23. #include "snow.h"
  24. #include "rangecoder.h"
  25. #include "mpegvideo.h"
  26. #undef NDEBUG
  27. #include <assert.h>
  28. static const int8_t quant3[256]={
  29. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  30. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  31. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  32. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  33. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  34. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  35. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  36. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  38. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  39. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  40. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  41. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  42. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  43. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  44. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
  45. };
  46. static const int8_t quant3b[256]={
  47. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  48. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  49. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  50. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  56. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  57. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  58. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  59. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  60. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  61. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  62. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  63. };
  64. static const int8_t quant3bA[256]={
  65. 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  66. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  67. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  68. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  69. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  70. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  71. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  72. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  73. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  74. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  75. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  76. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  77. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  78. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  79. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  80. 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
  81. };
  82. static const int8_t quant5[256]={
  83. 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  84. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  85. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  86. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  87. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  88. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  89. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  90. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  91. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  92. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  93. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  94. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  95. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  96. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  97. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  98. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
  99. };
  100. static const int8_t quant7[256]={
  101. 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  102. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  103. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  104. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  105. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  106. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  107. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  108. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  109. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  110. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  111. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  112. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  113. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  114. -3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,
  115. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  116. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
  117. };
  118. static const int8_t quant9[256]={
  119. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  120. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  121. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  122. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  123. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  124. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  125. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  126. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  127. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  128. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  129. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  130. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  131. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  132. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  133. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  134. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  135. };
  136. static const int8_t quant11[256]={
  137. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  138. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  139. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  140. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  141. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  142. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  143. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  144. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  145. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  146. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  147. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  148. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  149. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  150. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
  151. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  152. -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
  153. };
  154. static const int8_t quant13[256]={
  155. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  156. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  157. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  158. 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  159. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  160. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  161. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  162. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  163. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  164. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  165. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  166. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  167. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  168. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  169. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  170. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  171. };
  172. #if 0 //64*cubic
  173. static const uint8_t obmc32[1024]={
  174. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  175. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  176. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  177. 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
  178. 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
  179. 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
  180. 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
  181. 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
  182. 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
  183. 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
  184. 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
  185. 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
  186. 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
  187. 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
  188. 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
  189. 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
  190. 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
  191. 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
  192. 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
  193. 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
  194. 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
  195. 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
  196. 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
  197. 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
  198. 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
  199. 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
  200. 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
  201. 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
  202. 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
  203. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  204. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  205. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  206. //error:0.000022
  207. };
  208. static const uint8_t obmc16[256]={
  209. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  210. 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
  211. 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
  212. 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
  213. 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
  214. 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
  215. 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
  216. 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
  217. 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
  218. 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
  219. 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
  220. 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
  221. 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
  222. 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
  223. 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
  224. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  225. //error:0.000033
  226. };
  227. #elif 1 // 64*linear
  228. static const uint8_t obmc32[1024]={
  229. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  230. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  231. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  232. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  233. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  234. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  235. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  236. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  237. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  238. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  239. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  240. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  241. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  242. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  243. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  244. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  245. 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
  246. 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
  247. 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
  248. 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
  249. 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
  250. 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
  251. 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
  252. 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
  253. 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
  254. 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
  255. 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
  256. 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
  257. 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
  258. 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
  259. 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
  260. 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
  261. //error:0.000020
  262. };
  263. static const uint8_t obmc16[256]={
  264. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  265. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  266. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  267. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  268. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  269. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  270. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  271. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  272. 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
  273. 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
  274. 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
  275. 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
  276. 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
  277. 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
  278. 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
  279. 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
  280. //error:0.000015
  281. };
  282. #else //64*cos
  283. static const uint8_t obmc32[1024]={
  284. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  285. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  286. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  287. 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
  288. 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
  289. 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
  290. 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
  291. 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
  292. 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
  293. 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
  294. 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
  295. 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
  296. 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
  297. 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
  298. 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
  299. 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
  300. 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
  301. 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
  302. 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
  303. 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
  304. 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
  305. 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
  306. 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
  307. 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
  308. 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
  309. 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
  310. 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
  311. 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
  312. 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
  313. 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
  314. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  315. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  316. //error:0.000022
  317. };
  318. static const uint8_t obmc16[256]={
  319. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  320. 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
  321. 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
  322. 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
  323. 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
  324. 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
  325. 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
  326. 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
  327. 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
  328. 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
  329. 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
  330. 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
  331. 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
  332. 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
  333. 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
  334. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  335. //error:0.000022
  336. };
  337. #endif
  338. //linear *64
  339. static const uint8_t obmc8[64]={
  340. 4, 12, 20, 28, 28, 20, 12, 4,
  341. 12, 36, 60, 84, 84, 60, 36, 12,
  342. 20, 60,100,140,140,100, 60, 20,
  343. 28, 84,140,196,196,140, 84, 28,
  344. 28, 84,140,196,196,140, 84, 28,
  345. 20, 60,100,140,140,100, 60, 20,
  346. 12, 36, 60, 84, 84, 60, 36, 12,
  347. 4, 12, 20, 28, 28, 20, 12, 4,
  348. //error:0.000000
  349. };
  350. //linear *64
  351. static const uint8_t obmc4[16]={
  352. 16, 48, 48, 16,
  353. 48,144,144, 48,
  354. 48,144,144, 48,
  355. 16, 48, 48, 16,
  356. //error:0.000000
  357. };
  358. static const uint8_t *obmc_tab[4]={
  359. obmc32, obmc16, obmc8, obmc4
  360. };
  361. static int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
  362. typedef struct BlockNode{
  363. int16_t mx;
  364. int16_t my;
  365. uint8_t ref;
  366. uint8_t color[3];
  367. uint8_t type;
  368. //#define TYPE_SPLIT 1
  369. #define BLOCK_INTRA 1
  370. #define BLOCK_OPT 2
  371. //#define TYPE_NOCOLOR 4
  372. uint8_t level; //FIXME merge into type?
  373. }BlockNode;
  374. static const BlockNode null_block= { //FIXME add border maybe
  375. .color= {128,128,128},
  376. .mx= 0,
  377. .my= 0,
  378. .ref= 0,
  379. .type= 0,
  380. .level= 0,
  381. };
  382. #define LOG2_MB_SIZE 4
  383. #define MB_SIZE (1<<LOG2_MB_SIZE)
  384. typedef struct x_and_coeff{
  385. int16_t x;
  386. uint16_t coeff;
  387. } x_and_coeff;
  388. typedef struct SubBand{
  389. int level;
  390. int stride;
  391. int width;
  392. int height;
  393. int qlog; ///< log(qscale)/log[2^(1/6)]
  394. DWTELEM *buf;
  395. int buf_x_offset;
  396. int buf_y_offset;
  397. int stride_line; ///< Stride measured in lines, not pixels.
  398. x_and_coeff * x_coeff;
  399. struct SubBand *parent;
  400. uint8_t state[/*7*2*/ 7 + 512][32];
  401. }SubBand;
  402. typedef struct Plane{
  403. int width;
  404. int height;
  405. SubBand band[MAX_DECOMPOSITIONS][4];
  406. }Plane;
  407. typedef struct SnowContext{
  408. // MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
  409. AVCodecContext *avctx;
  410. RangeCoder c;
  411. DSPContext dsp;
  412. AVFrame new_picture;
  413. AVFrame input_picture; ///< new_picture with the internal linesizes
  414. AVFrame current_picture;
  415. AVFrame last_picture[MAX_REF_FRAMES];
  416. AVFrame mconly_picture;
  417. // uint8_t q_context[16];
  418. uint8_t header_state[32];
  419. uint8_t block_state[128 + 32*128];
  420. int keyframe;
  421. int always_reset;
  422. int version;
  423. int spatial_decomposition_type;
  424. int 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 independant 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= 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. BlockNode *left, BlockNode *top, 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. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1774. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1775. BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  1776. BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  1777. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1778. 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. s->m.me.skip= 0;
  1822. assert(s->m.me. stride == stride);
  1823. assert(s->m.me.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= s->m.me.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. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1974. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1975. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1976. 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. BlockNode *left = x ? &s->block[index-1] : &null_block;
  2024. BlockNode *top = y ? &s->block[index-w] : &null_block;
  2025. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  2026. 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;
  2034. int l = left->color[0];
  2035. int cb= left->color[1];
  2036. int cr= left->color[2];
  2037. int mx= mid_pred(left->mx, top->mx, tr->mx);
  2038. int my= mid_pred(left->my, top->my, tr->my);
  2039. int ref = 0;
  2040. int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
  2041. int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
  2042. int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
  2043. type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  2044. if(type){
  2045. pred_mv(s, &mx, &my, 0, left, top, tr);
  2046. l += get_symbol(&s->c, &s->block_state[32], 1);
  2047. cb+= get_symbol(&s->c, &s->block_state[64], 1);
  2048. cr+= get_symbol(&s->c, &s->block_state[96], 1);
  2049. }else{
  2050. if(s->ref_frames > 1)
  2051. ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
  2052. pred_mv(s, &mx, &my, ref, left, top, tr);
  2053. mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
  2054. my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
  2055. }
  2056. set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
  2057. }else{
  2058. decode_q_branch(s, level+1, 2*x+0, 2*y+0);
  2059. decode_q_branch(s, level+1, 2*x+1, 2*y+0);
  2060. decode_q_branch(s, level+1, 2*x+0, 2*y+1);
  2061. decode_q_branch(s, level+1, 2*x+1, 2*y+1);
  2062. }
  2063. }
  2064. static void encode_blocks(SnowContext *s, int search){
  2065. int x, y;
  2066. int w= s->b_width;
  2067. int h= s->b_height;
  2068. if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
  2069. iterative_me(s);
  2070. for(y=0; y<h; y++){
  2071. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  2072. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  2073. return;
  2074. }
  2075. for(x=0; x<w; x++){
  2076. if(s->avctx->me_method == ME_ITER || !search)
  2077. encode_q_branch2(s, 0, x, y);
  2078. else
  2079. encode_q_branch (s, 0, x, y);
  2080. }
  2081. }
  2082. }
  2083. static void decode_blocks(SnowContext *s){
  2084. int x, y;
  2085. int w= s->b_width;
  2086. int h= s->b_height;
  2087. for(y=0; y<h; y++){
  2088. for(x=0; x<w; x++){
  2089. decode_q_branch(s, 0, x, y);
  2090. }
  2091. }
  2092. }
  2093. 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){
  2094. int x, y;
  2095. START_TIMER
  2096. for(y=0; y < b_h+5; y++){
  2097. for(x=0; x < b_w; x++){
  2098. int a0= src[x ];
  2099. int a1= src[x + 1];
  2100. int a2= src[x + 2];
  2101. int a3= src[x + 3];
  2102. int a4= src[x + 4];
  2103. int a5= src[x + 5];
  2104. // int am= 9*(a1+a2) - (a0+a3);
  2105. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  2106. // int am= 18*(a2+a3) - 2*(a1+a4);
  2107. // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  2108. // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
  2109. // if(b_w==16) am= 8*(a1+a2);
  2110. if(dx<8) am = (32*a2*( 8-dx) + am* dx + 128)>>8;
  2111. else am = ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
  2112. /* FIXME Try increasing tmp buffer to 16 bits and not clipping here. Should give marginally better results. - Robert*/
  2113. if(am&(~255)) am= ~(am>>31);
  2114. tmp[x] = am;
  2115. /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
  2116. else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
  2117. else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
  2118. else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
  2119. }
  2120. tmp += stride;
  2121. src += stride;
  2122. }
  2123. tmp -= (b_h+5)*stride;
  2124. for(y=0; y < b_h; y++){
  2125. for(x=0; x < b_w; x++){
  2126. int a0= tmp[x + 0*stride];
  2127. int a1= tmp[x + 1*stride];
  2128. int a2= tmp[x + 2*stride];
  2129. int a3= tmp[x + 3*stride];
  2130. int a4= tmp[x + 4*stride];
  2131. int a5= tmp[x + 5*stride];
  2132. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  2133. // int am= 18*(a2+a3) - 2*(a1+a4);
  2134. /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  2135. int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
  2136. // if(b_w==16) am= 8*(a1+a2);
  2137. if(dy<8) am = (32*a2*( 8-dy) + am* dy + 128)>>8;
  2138. else am = ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
  2139. if(am&(~255)) am= ~(am>>31);
  2140. dst[x] = am;
  2141. /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
  2142. else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
  2143. else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
  2144. else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
  2145. }
  2146. dst += stride;
  2147. tmp += stride;
  2148. }
  2149. STOP_TIMER("mc_block")
  2150. }
  2151. #define mca(dx,dy,b_w)\
  2152. static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, uint8_t *src, int stride, int h){\
  2153. uint8_t tmp[stride*(b_w+5)];\
  2154. assert(h==b_w);\
  2155. mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
  2156. }
  2157. mca( 0, 0,16)
  2158. mca( 8, 0,16)
  2159. mca( 0, 8,16)
  2160. mca( 8, 8,16)
  2161. mca( 0, 0,8)
  2162. mca( 8, 0,8)
  2163. mca( 0, 8,8)
  2164. mca( 8, 8,8)
  2165. 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){
  2166. if(block->type & BLOCK_INTRA){
  2167. int x, y;
  2168. const int color = block->color[plane_index];
  2169. const int color4= color*0x01010101;
  2170. if(b_w==32){
  2171. for(y=0; y < b_h; y++){
  2172. *(uint32_t*)&dst[0 + y*stride]= color4;
  2173. *(uint32_t*)&dst[4 + y*stride]= color4;
  2174. *(uint32_t*)&dst[8 + y*stride]= color4;
  2175. *(uint32_t*)&dst[12+ y*stride]= color4;
  2176. *(uint32_t*)&dst[16+ y*stride]= color4;
  2177. *(uint32_t*)&dst[20+ y*stride]= color4;
  2178. *(uint32_t*)&dst[24+ y*stride]= color4;
  2179. *(uint32_t*)&dst[28+ y*stride]= color4;
  2180. }
  2181. }else if(b_w==16){
  2182. for(y=0; y < b_h; y++){
  2183. *(uint32_t*)&dst[0 + y*stride]= color4;
  2184. *(uint32_t*)&dst[4 + y*stride]= color4;
  2185. *(uint32_t*)&dst[8 + y*stride]= color4;
  2186. *(uint32_t*)&dst[12+ y*stride]= color4;
  2187. }
  2188. }else if(b_w==8){
  2189. for(y=0; y < b_h; y++){
  2190. *(uint32_t*)&dst[0 + y*stride]= color4;
  2191. *(uint32_t*)&dst[4 + y*stride]= color4;
  2192. }
  2193. }else if(b_w==4){
  2194. for(y=0; y < b_h; y++){
  2195. *(uint32_t*)&dst[0 + y*stride]= color4;
  2196. }
  2197. }else{
  2198. for(y=0; y < b_h; y++){
  2199. for(x=0; x < b_w; x++){
  2200. dst[x + y*stride]= color;
  2201. }
  2202. }
  2203. }
  2204. }else{
  2205. uint8_t *src= s->last_picture[block->ref].data[plane_index];
  2206. const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
  2207. int mx= block->mx*scale;
  2208. int my= block->my*scale;
  2209. const int dx= mx&15;
  2210. const int dy= my&15;
  2211. const int tab_index= 3 - (b_w>>2) + (b_w>>4);
  2212. sx += (mx>>4) - 2;
  2213. sy += (my>>4) - 2;
  2214. src += sx + sy*stride;
  2215. if( (unsigned)sx >= w - b_w - 4
  2216. || (unsigned)sy >= h - b_h - 4){
  2217. ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+5, b_h+5, sx, sy, w, h);
  2218. src= tmp + MB_SIZE;
  2219. }
  2220. // assert(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h);
  2221. // assert(!(b_w&(b_w-1)));
  2222. assert(b_w>1 && b_h>1);
  2223. assert(tab_index>=0 && tab_index<4 || b_w==32);
  2224. if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)))
  2225. mc_block(dst, src, tmp, stride, b_w, b_h, dx, dy);
  2226. else if(b_w==32){
  2227. int y;
  2228. for(y=0; y<b_h; y+=16){
  2229. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 2 + (y+2)*stride,stride);
  2230. s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 18 + (y+2)*stride,stride);
  2231. }
  2232. }else if(b_w==b_h)
  2233. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 2 + 2*stride,stride);
  2234. else if(b_w==2*b_h){
  2235. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 2 + 2*stride,stride);
  2236. s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 2 + b_h + 2*stride,stride);
  2237. }else{
  2238. assert(2*b_w==b_h);
  2239. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 2 + 2*stride ,stride);
  2240. s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 2 + 2*stride+b_w*stride,stride);
  2241. }
  2242. }
  2243. }
  2244. void ff_snow_inner_add_yblock(uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
  2245. int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
  2246. int y, x;
  2247. DWTELEM * dst;
  2248. for(y=0; y<b_h; y++){
  2249. //FIXME ugly missue of obmc_stride
  2250. uint8_t *obmc1= obmc + y*obmc_stride;
  2251. uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2252. uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2253. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2254. dst = slice_buffer_get_line(sb, src_y + y);
  2255. for(x=0; x<b_w; x++){
  2256. int v= obmc1[x] * block[3][x + y*src_stride]
  2257. +obmc2[x] * block[2][x + y*src_stride]
  2258. +obmc3[x] * block[1][x + y*src_stride]
  2259. +obmc4[x] * block[0][x + y*src_stride];
  2260. v <<= 8 - LOG2_OBMC_MAX;
  2261. if(FRAC_BITS != 8){
  2262. v += 1<<(7 - FRAC_BITS);
  2263. v >>= 8 - FRAC_BITS;
  2264. }
  2265. if(add){
  2266. v += dst[x + src_x];
  2267. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2268. if(v&(~255)) v= ~(v>>31);
  2269. dst8[x + y*src_stride] = v;
  2270. }else{
  2271. dst[x + src_x] -= v;
  2272. }
  2273. }
  2274. }
  2275. }
  2276. //FIXME name clenup (b_w, block_w, b_width stuff)
  2277. 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){
  2278. const int b_width = s->b_width << s->block_max_depth;
  2279. const int b_height= s->b_height << s->block_max_depth;
  2280. const int b_stride= b_width;
  2281. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  2282. BlockNode *rt= lt+1;
  2283. BlockNode *lb= lt+b_stride;
  2284. BlockNode *rb= lb+1;
  2285. uint8_t *block[4];
  2286. int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
  2287. uint8_t tmp[src_stride*7*MB_SIZE]; //FIXME align
  2288. uint8_t *ptmp;
  2289. int x,y;
  2290. if(b_x<0){
  2291. lt= rt;
  2292. lb= rb;
  2293. }else if(b_x + 1 >= b_width){
  2294. rt= lt;
  2295. rb= lb;
  2296. }
  2297. if(b_y<0){
  2298. lt= lb;
  2299. rt= rb;
  2300. }else if(b_y + 1 >= b_height){
  2301. lb= lt;
  2302. rb= rt;
  2303. }
  2304. if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
  2305. obmc -= src_x;
  2306. b_w += src_x;
  2307. if(!sliced && !offset_dst)
  2308. dst -= src_x;
  2309. src_x=0;
  2310. }else if(src_x + b_w > w){
  2311. b_w = w - src_x;
  2312. }
  2313. if(src_y<0){
  2314. obmc -= src_y*obmc_stride;
  2315. b_h += src_y;
  2316. if(!sliced && !offset_dst)
  2317. dst -= src_y*dst_stride;
  2318. src_y=0;
  2319. }else if(src_y + b_h> h){
  2320. b_h = h - src_y;
  2321. }
  2322. if(b_w<=0 || b_h<=0) return;
  2323. assert(src_stride > 2*MB_SIZE + 5);
  2324. if(!sliced && offset_dst)
  2325. dst += src_x + src_y*dst_stride;
  2326. dst8+= src_x + src_y*src_stride;
  2327. // src += src_x + src_y*src_stride;
  2328. ptmp= tmp + 3*tmp_step;
  2329. block[0]= ptmp;
  2330. ptmp+=tmp_step;
  2331. pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  2332. if(same_block(lt, rt)){
  2333. block[1]= block[0];
  2334. }else{
  2335. block[1]= ptmp;
  2336. ptmp+=tmp_step;
  2337. pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  2338. }
  2339. if(same_block(lt, lb)){
  2340. block[2]= block[0];
  2341. }else if(same_block(rt, lb)){
  2342. block[2]= block[1];
  2343. }else{
  2344. block[2]= ptmp;
  2345. ptmp+=tmp_step;
  2346. pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  2347. }
  2348. if(same_block(lt, rb) ){
  2349. block[3]= block[0];
  2350. }else if(same_block(rt, rb)){
  2351. block[3]= block[1];
  2352. }else if(same_block(lb, rb)){
  2353. block[3]= block[2];
  2354. }else{
  2355. block[3]= ptmp;
  2356. pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  2357. }
  2358. #if 0
  2359. for(y=0; y<b_h; y++){
  2360. for(x=0; x<b_w; x++){
  2361. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  2362. if(add) dst[x + y*dst_stride] += v;
  2363. else dst[x + y*dst_stride] -= v;
  2364. }
  2365. }
  2366. for(y=0; y<b_h; y++){
  2367. uint8_t *obmc2= obmc + (obmc_stride>>1);
  2368. for(x=0; x<b_w; x++){
  2369. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  2370. if(add) dst[x + y*dst_stride] += v;
  2371. else dst[x + y*dst_stride] -= v;
  2372. }
  2373. }
  2374. for(y=0; y<b_h; y++){
  2375. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2376. for(x=0; x<b_w; x++){
  2377. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  2378. if(add) dst[x + y*dst_stride] += v;
  2379. else dst[x + y*dst_stride] -= v;
  2380. }
  2381. }
  2382. for(y=0; y<b_h; y++){
  2383. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2384. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2385. for(x=0; x<b_w; x++){
  2386. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  2387. if(add) dst[x + y*dst_stride] += v;
  2388. else dst[x + y*dst_stride] -= v;
  2389. }
  2390. }
  2391. #else
  2392. if(sliced){
  2393. START_TIMER
  2394. s->dsp.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
  2395. STOP_TIMER("inner_add_yblock")
  2396. }else
  2397. for(y=0; y<b_h; y++){
  2398. //FIXME ugly missue of obmc_stride
  2399. uint8_t *obmc1= obmc + y*obmc_stride;
  2400. uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2401. uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2402. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2403. for(x=0; x<b_w; x++){
  2404. int v= obmc1[x] * block[3][x + y*src_stride]
  2405. +obmc2[x] * block[2][x + y*src_stride]
  2406. +obmc3[x] * block[1][x + y*src_stride]
  2407. +obmc4[x] * block[0][x + y*src_stride];
  2408. v <<= 8 - LOG2_OBMC_MAX;
  2409. if(FRAC_BITS != 8){
  2410. v += 1<<(7 - FRAC_BITS);
  2411. v >>= 8 - FRAC_BITS;
  2412. }
  2413. if(add){
  2414. v += dst[x + y*dst_stride];
  2415. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2416. if(v&(~255)) v= ~(v>>31);
  2417. dst8[x + y*src_stride] = v;
  2418. }else{
  2419. dst[x + y*dst_stride] -= v;
  2420. }
  2421. }
  2422. }
  2423. #endif
  2424. }
  2425. static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, DWTELEM * old_buffer, int plane_index, int add, int mb_y){
  2426. Plane *p= &s->plane[plane_index];
  2427. const int mb_w= s->b_width << s->block_max_depth;
  2428. const int mb_h= s->b_height << s->block_max_depth;
  2429. int x, y, mb_x;
  2430. int block_size = MB_SIZE >> s->block_max_depth;
  2431. int block_w = plane_index ? block_size/2 : block_size;
  2432. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2433. int obmc_stride= plane_index ? block_size : 2*block_size;
  2434. int ref_stride= s->current_picture.linesize[plane_index];
  2435. uint8_t *dst8= s->current_picture.data[plane_index];
  2436. int w= p->width;
  2437. int h= p->height;
  2438. START_TIMER
  2439. if(s->keyframe || (s->avctx->debug&512)){
  2440. if(mb_y==mb_h)
  2441. return;
  2442. if(add){
  2443. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++)
  2444. {
  2445. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2446. DWTELEM * line = sb->line[y];
  2447. for(x=0; x<w; x++)
  2448. {
  2449. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2450. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2451. v >>= FRAC_BITS;
  2452. if(v&(~255)) v= ~(v>>31);
  2453. dst8[x + y*ref_stride]= v;
  2454. }
  2455. }
  2456. }else{
  2457. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++)
  2458. {
  2459. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2460. DWTELEM * line = sb->line[y];
  2461. for(x=0; x<w; x++)
  2462. {
  2463. line[x] -= 128 << FRAC_BITS;
  2464. // buf[x + y*w]-= 128<<FRAC_BITS;
  2465. }
  2466. }
  2467. }
  2468. return;
  2469. }
  2470. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2471. START_TIMER
  2472. add_yblock(s, 1, sb, old_buffer, dst8, obmc,
  2473. block_w*mb_x - block_w/2,
  2474. block_w*mb_y - block_w/2,
  2475. block_w, block_w,
  2476. w, h,
  2477. w, ref_stride, obmc_stride,
  2478. mb_x - 1, mb_y - 1,
  2479. add, 0, plane_index);
  2480. STOP_TIMER("add_yblock")
  2481. }
  2482. STOP_TIMER("predict_slice")
  2483. }
  2484. static av_always_inline void predict_slice(SnowContext *s, DWTELEM *buf, int plane_index, int add, int mb_y){
  2485. Plane *p= &s->plane[plane_index];
  2486. const int mb_w= s->b_width << s->block_max_depth;
  2487. const int mb_h= s->b_height << s->block_max_depth;
  2488. int x, y, mb_x;
  2489. int block_size = MB_SIZE >> s->block_max_depth;
  2490. int block_w = plane_index ? block_size/2 : block_size;
  2491. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2492. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2493. int ref_stride= s->current_picture.linesize[plane_index];
  2494. uint8_t *dst8= s->current_picture.data[plane_index];
  2495. int w= p->width;
  2496. int h= p->height;
  2497. START_TIMER
  2498. if(s->keyframe || (s->avctx->debug&512)){
  2499. if(mb_y==mb_h)
  2500. return;
  2501. if(add){
  2502. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2503. for(x=0; x<w; x++){
  2504. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2505. v >>= FRAC_BITS;
  2506. if(v&(~255)) v= ~(v>>31);
  2507. dst8[x + y*ref_stride]= v;
  2508. }
  2509. }
  2510. }else{
  2511. for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
  2512. for(x=0; x<w; x++){
  2513. buf[x + y*w]-= 128<<FRAC_BITS;
  2514. }
  2515. }
  2516. }
  2517. return;
  2518. }
  2519. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2520. START_TIMER
  2521. add_yblock(s, 0, NULL, buf, dst8, obmc,
  2522. block_w*mb_x - block_w/2,
  2523. block_w*mb_y - block_w/2,
  2524. block_w, block_w,
  2525. w, h,
  2526. w, ref_stride, obmc_stride,
  2527. mb_x - 1, mb_y - 1,
  2528. add, 1, plane_index);
  2529. STOP_TIMER("add_yblock")
  2530. }
  2531. STOP_TIMER("predict_slice")
  2532. }
  2533. static av_always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
  2534. const int mb_h= s->b_height << s->block_max_depth;
  2535. int mb_y;
  2536. for(mb_y=0; mb_y<=mb_h; mb_y++)
  2537. predict_slice(s, buf, plane_index, add, mb_y);
  2538. }
  2539. static int get_dc(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2540. int i, x2, y2;
  2541. Plane *p= &s->plane[plane_index];
  2542. const int block_size = MB_SIZE >> s->block_max_depth;
  2543. const int block_w = plane_index ? block_size/2 : block_size;
  2544. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2545. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2546. const int ref_stride= s->current_picture.linesize[plane_index];
  2547. uint8_t *src= s-> input_picture.data[plane_index];
  2548. DWTELEM *dst= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2549. const int b_stride = s->b_width << s->block_max_depth;
  2550. const int w= p->width;
  2551. const int h= p->height;
  2552. int index= mb_x + mb_y*b_stride;
  2553. BlockNode *b= &s->block[index];
  2554. BlockNode backup= *b;
  2555. int ab=0;
  2556. int aa=0;
  2557. b->type|= BLOCK_INTRA;
  2558. b->color[plane_index]= 0;
  2559. memset(dst, 0, obmc_stride*obmc_stride*sizeof(DWTELEM));
  2560. for(i=0; i<4; i++){
  2561. int mb_x2= mb_x + (i &1) - 1;
  2562. int mb_y2= mb_y + (i>>1) - 1;
  2563. int x= block_w*mb_x2 + block_w/2;
  2564. int y= block_w*mb_y2 + block_w/2;
  2565. add_yblock(s, 0, NULL, dst + ((i&1)+(i>>1)*obmc_stride)*block_w, NULL, obmc,
  2566. x, y, block_w, block_w, w, h, obmc_stride, ref_stride, obmc_stride, mb_x2, mb_y2, 0, 0, plane_index);
  2567. for(y2= FFMAX(y, 0); y2<FFMIN(h, y+block_w); y2++){
  2568. for(x2= FFMAX(x, 0); x2<FFMIN(w, x+block_w); x2++){
  2569. int index= x2-(block_w*mb_x - block_w/2) + (y2-(block_w*mb_y - block_w/2))*obmc_stride;
  2570. int obmc_v= obmc[index];
  2571. int d;
  2572. if(y<0) obmc_v += obmc[index + block_w*obmc_stride];
  2573. if(x<0) obmc_v += obmc[index + block_w];
  2574. if(y+block_w>h) obmc_v += obmc[index - block_w*obmc_stride];
  2575. if(x+block_w>w) obmc_v += obmc[index - block_w];
  2576. //FIXME precalc this or simplify it somehow else
  2577. d = -dst[index] + (1<<(FRAC_BITS-1));
  2578. dst[index] = d;
  2579. ab += (src[x2 + y2*ref_stride] - (d>>FRAC_BITS)) * obmc_v;
  2580. aa += obmc_v * obmc_v; //FIXME precalclate this
  2581. }
  2582. }
  2583. }
  2584. *b= backup;
  2585. return clip(((ab<<LOG2_OBMC_MAX) + aa/2)/aa, 0, 255); //FIXME we shouldnt need cliping
  2586. }
  2587. static inline int get_block_bits(SnowContext *s, int x, int y, int w){
  2588. const int b_stride = s->b_width << s->block_max_depth;
  2589. const int b_height = s->b_height<< s->block_max_depth;
  2590. int index= x + y*b_stride;
  2591. BlockNode *b = &s->block[index];
  2592. BlockNode *left = x ? &s->block[index-1] : &null_block;
  2593. BlockNode *top = y ? &s->block[index-b_stride] : &null_block;
  2594. BlockNode *tl = y && x ? &s->block[index-b_stride-1] : left;
  2595. BlockNode *tr = y && x+w<b_stride ? &s->block[index-b_stride+w] : tl;
  2596. int dmx, dmy;
  2597. // int mx_context= av_log2(2*FFABS(left->mx - top->mx));
  2598. // int my_context= av_log2(2*FFABS(left->my - top->my));
  2599. if(x<0 || x>=b_stride || y>=b_height)
  2600. return 0;
  2601. /*
  2602. 1 0 0
  2603. 01X 1-2 1
  2604. 001XX 3-6 2-3
  2605. 0001XXX 7-14 4-7
  2606. 00001XXXX 15-30 8-15
  2607. */
  2608. //FIXME try accurate rate
  2609. //FIXME intra and inter predictors if surrounding blocks arent the same type
  2610. if(b->type & BLOCK_INTRA){
  2611. return 3+2*( av_log2(2*FFABS(left->color[0] - b->color[0]))
  2612. + av_log2(2*FFABS(left->color[1] - b->color[1]))
  2613. + av_log2(2*FFABS(left->color[2] - b->color[2])));
  2614. }else{
  2615. pred_mv(s, &dmx, &dmy, b->ref, left, top, tr);
  2616. dmx-= b->mx;
  2617. dmy-= b->my;
  2618. return 2*(1 + av_log2(2*FFABS(dmx)) //FIXME kill the 2* can be merged in lambda
  2619. + av_log2(2*FFABS(dmy))
  2620. + av_log2(2*b->ref));
  2621. }
  2622. }
  2623. static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){
  2624. Plane *p= &s->plane[plane_index];
  2625. const int block_size = MB_SIZE >> s->block_max_depth;
  2626. const int block_w = plane_index ? block_size/2 : block_size;
  2627. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2628. const int ref_stride= s->current_picture.linesize[plane_index];
  2629. uint8_t *dst= s->current_picture.data[plane_index];
  2630. uint8_t *src= s-> input_picture.data[plane_index];
  2631. DWTELEM *pred= (DWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;
  2632. uint8_t cur[ref_stride*2*MB_SIZE]; //FIXME alignment
  2633. uint8_t tmp[ref_stride*(2*MB_SIZE+5)];
  2634. const int b_stride = s->b_width << s->block_max_depth;
  2635. const int b_height = s->b_height<< s->block_max_depth;
  2636. const int w= p->width;
  2637. const int h= p->height;
  2638. int distortion;
  2639. int rate= 0;
  2640. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2641. int sx= block_w*mb_x - block_w/2;
  2642. int sy= block_w*mb_y - block_w/2;
  2643. int x0= FFMAX(0,-sx);
  2644. int y0= FFMAX(0,-sy);
  2645. int x1= FFMIN(block_w*2, w-sx);
  2646. int y1= FFMIN(block_w*2, h-sy);
  2647. int i,x,y;
  2648. 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);
  2649. for(y=y0; y<y1; y++){
  2650. const uint8_t *obmc1= obmc_edged + y*obmc_stride;
  2651. const DWTELEM *pred1 = pred + y*obmc_stride;
  2652. uint8_t *cur1 = cur + y*ref_stride;
  2653. uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;
  2654. for(x=x0; x<x1; x++){
  2655. int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);
  2656. v = (v + pred1[x]) >> FRAC_BITS;
  2657. if(v&(~255)) v= ~(v>>31);
  2658. dst1[x] = v;
  2659. }
  2660. }
  2661. /* copy the regions where obmc[] = (uint8_t)256 */
  2662. if(LOG2_OBMC_MAX == 8
  2663. && (mb_x == 0 || mb_x == b_stride-1)
  2664. && (mb_y == 0 || mb_y == b_height-1)){
  2665. if(mb_x == 0)
  2666. x1 = block_w;
  2667. else
  2668. x0 = block_w;
  2669. if(mb_y == 0)
  2670. y1 = block_w;
  2671. else
  2672. y0 = block_w;
  2673. for(y=y0; y<y1; y++)
  2674. memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);
  2675. }
  2676. if(block_w==16){
  2677. /* FIXME rearrange dsputil to fit 32x32 cmp functions */
  2678. /* FIXME check alignment of the cmp wavelet vs the encoding wavelet */
  2679. /* FIXME cmps overlap but don't cover the wavelet's whole support,
  2680. * so improving the score of one block is not strictly guaranteed to
  2681. * improve the score of the whole frame, so iterative motion est
  2682. * doesn't always converge. */
  2683. if(s->avctx->me_cmp == FF_CMP_W97)
  2684. distortion = w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2685. else if(s->avctx->me_cmp == FF_CMP_W53)
  2686. distortion = w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);
  2687. else{
  2688. distortion = 0;
  2689. for(i=0; i<4; i++){
  2690. int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
  2691. distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);
  2692. }
  2693. }
  2694. }else{
  2695. assert(block_w==8);
  2696. distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
  2697. }
  2698. if(plane_index==0){
  2699. for(i=0; i<4; i++){
  2700. /* ..RRr
  2701. * .RXx.
  2702. * rxx..
  2703. */
  2704. rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);
  2705. }
  2706. if(mb_x == b_stride-2)
  2707. rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);
  2708. }
  2709. return distortion + rate*penalty_factor;
  2710. }
  2711. static int get_4block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index){
  2712. int i, y2;
  2713. Plane *p= &s->plane[plane_index];
  2714. const int block_size = MB_SIZE >> s->block_max_depth;
  2715. const int block_w = plane_index ? block_size/2 : block_size;
  2716. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2717. const int obmc_stride= plane_index ? block_size : 2*block_size;
  2718. const int ref_stride= s->current_picture.linesize[plane_index];
  2719. uint8_t *dst= s->current_picture.data[plane_index];
  2720. uint8_t *src= s-> input_picture.data[plane_index];
  2721. static const DWTELEM zero_dst[4096]; //FIXME
  2722. const int b_stride = s->b_width << s->block_max_depth;
  2723. const int w= p->width;
  2724. const int h= p->height;
  2725. int distortion= 0;
  2726. int rate= 0;
  2727. const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);
  2728. for(i=0; i<9; i++){
  2729. int mb_x2= mb_x + (i%3) - 1;
  2730. int mb_y2= mb_y + (i/3) - 1;
  2731. int x= block_w*mb_x2 + block_w/2;
  2732. int y= block_w*mb_y2 + block_w/2;
  2733. add_yblock(s, 0, NULL, zero_dst, dst, obmc,
  2734. x, y, block_w, block_w, w, h, /*dst_stride*/0, ref_stride, obmc_stride, mb_x2, mb_y2, 1, 1, plane_index);
  2735. //FIXME find a cleaner/simpler way to skip the outside stuff
  2736. for(y2= y; y2<0; y2++)
  2737. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2738. for(y2= h; y2<y+block_w; y2++)
  2739. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, block_w);
  2740. if(x<0){
  2741. for(y2= y; y2<y+block_w; y2++)
  2742. memcpy(dst + x + y2*ref_stride, src + x + y2*ref_stride, -x);
  2743. }
  2744. if(x+block_w > w){
  2745. for(y2= y; y2<y+block_w; y2++)
  2746. memcpy(dst + w + y2*ref_stride, src + w + y2*ref_stride, x+block_w - w);
  2747. }
  2748. assert(block_w== 8 || block_w==16);
  2749. distortion += s->dsp.me_cmp[block_w==8](&s->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_w);
  2750. }
  2751. if(plane_index==0){
  2752. BlockNode *b= &s->block[mb_x+mb_y*b_stride];
  2753. int merged= same_block(b,b+1) && same_block(b,b+b_stride) && same_block(b,b+b_stride+1);
  2754. /* ..RRRr
  2755. * .RXXx.
  2756. * .RXXx.
  2757. * rxxx.
  2758. */
  2759. if(merged)
  2760. rate = get_block_bits(s, mb_x, mb_y, 2);
  2761. for(i=merged?4:0; i<9; i++){
  2762. static const int dxy[9][2] = {{0,0},{1,0},{0,1},{1,1},{2,0},{2,1},{-1,2},{0,2},{1,2}};
  2763. rate += get_block_bits(s, mb_x + dxy[i][0], mb_y + dxy[i][1], 1);
  2764. }
  2765. }
  2766. return distortion + rate*penalty_factor;
  2767. }
  2768. 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){
  2769. const int b_stride= s->b_width << s->block_max_depth;
  2770. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2771. BlockNode backup= *block;
  2772. int rd, index, value;
  2773. assert(mb_x>=0 && mb_y>=0);
  2774. assert(mb_x<b_stride);
  2775. if(intra){
  2776. block->color[0] = p[0];
  2777. block->color[1] = p[1];
  2778. block->color[2] = p[2];
  2779. block->type |= BLOCK_INTRA;
  2780. }else{
  2781. index= (p[0] + 31*p[1]) & (ME_CACHE_SIZE-1);
  2782. value= s->me_cache_generation + (p[0]>>10) + (p[1]<<6) + (block->ref<<12);
  2783. if(s->me_cache[index] == value)
  2784. return 0;
  2785. s->me_cache[index]= value;
  2786. block->mx= p[0];
  2787. block->my= p[1];
  2788. block->type &= ~BLOCK_INTRA;
  2789. }
  2790. rd= get_block_rd(s, mb_x, mb_y, 0, obmc_edged);
  2791. //FIXME chroma
  2792. if(rd < *best_rd){
  2793. *best_rd= rd;
  2794. return 1;
  2795. }else{
  2796. *block= backup;
  2797. return 0;
  2798. }
  2799. }
  2800. /* special case for int[2] args we discard afterward, fixes compilation prob with gcc 2.95 */
  2801. 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){
  2802. int p[2] = {p0, p1};
  2803. return check_block(s, mb_x, mb_y, p, 0, obmc_edged, best_rd);
  2804. }
  2805. 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){
  2806. const int b_stride= s->b_width << s->block_max_depth;
  2807. BlockNode *block= &s->block[mb_x + mb_y * b_stride];
  2808. BlockNode backup[4]= {block[0], block[1], block[b_stride], block[b_stride+1]};
  2809. int rd, index, value;
  2810. assert(mb_x>=0 && mb_y>=0);
  2811. assert(mb_x<b_stride);
  2812. assert(((mb_x|mb_y)&1) == 0);
  2813. index= (p0 + 31*p1) & (ME_CACHE_SIZE-1);
  2814. value= s->me_cache_generation + (p0>>10) + (p1<<6) + (block->ref<<12);
  2815. if(s->me_cache[index] == value)
  2816. return 0;
  2817. s->me_cache[index]= value;
  2818. block->mx= p0;
  2819. block->my= p1;
  2820. block->ref= ref;
  2821. block->type &= ~BLOCK_INTRA;
  2822. block[1]= block[b_stride]= block[b_stride+1]= *block;
  2823. rd= get_4block_rd(s, mb_x, mb_y, 0);
  2824. //FIXME chroma
  2825. if(rd < *best_rd){
  2826. *best_rd= rd;
  2827. return 1;
  2828. }else{
  2829. block[0]= backup[0];
  2830. block[1]= backup[1];
  2831. block[b_stride]= backup[2];
  2832. block[b_stride+1]= backup[3];
  2833. return 0;
  2834. }
  2835. }
  2836. static void iterative_me(SnowContext *s){
  2837. int pass, mb_x, mb_y;
  2838. const int b_width = s->b_width << s->block_max_depth;
  2839. const int b_height= s->b_height << s->block_max_depth;
  2840. const int b_stride= b_width;
  2841. int color[3];
  2842. {
  2843. RangeCoder r = s->c;
  2844. uint8_t state[sizeof(s->block_state)];
  2845. memcpy(state, s->block_state, sizeof(s->block_state));
  2846. for(mb_y= 0; mb_y<s->b_height; mb_y++)
  2847. for(mb_x= 0; mb_x<s->b_width; mb_x++)
  2848. encode_q_branch(s, 0, mb_x, mb_y);
  2849. s->c = r;
  2850. memcpy(s->block_state, state, sizeof(s->block_state));
  2851. }
  2852. for(pass=0; pass<25; pass++){
  2853. int change= 0;
  2854. for(mb_y= 0; mb_y<b_height; mb_y++){
  2855. for(mb_x= 0; mb_x<b_width; mb_x++){
  2856. int dia_change, i, j, ref;
  2857. int best_rd= INT_MAX, ref_rd;
  2858. BlockNode backup, ref_b;
  2859. const int index= mb_x + mb_y * b_stride;
  2860. BlockNode *block= &s->block[index];
  2861. BlockNode *tb = mb_y ? &s->block[index-b_stride ] : NULL;
  2862. BlockNode *lb = mb_x ? &s->block[index -1] : NULL;
  2863. BlockNode *rb = mb_x+1<b_width ? &s->block[index +1] : NULL;
  2864. BlockNode *bb = mb_y+1<b_height ? &s->block[index+b_stride ] : NULL;
  2865. BlockNode *tlb= mb_x && mb_y ? &s->block[index-b_stride-1] : NULL;
  2866. BlockNode *trb= mb_x+1<b_width && mb_y ? &s->block[index-b_stride+1] : NULL;
  2867. BlockNode *blb= mb_x && mb_y+1<b_height ? &s->block[index+b_stride-1] : NULL;
  2868. BlockNode *brb= mb_x+1<b_width && mb_y+1<b_height ? &s->block[index+b_stride+1] : NULL;
  2869. const int b_w= (MB_SIZE >> s->block_max_depth);
  2870. uint8_t obmc_edged[b_w*2][b_w*2];
  2871. if(pass && (block->type & BLOCK_OPT))
  2872. continue;
  2873. block->type |= BLOCK_OPT;
  2874. backup= *block;
  2875. if(!s->me_cache_generation)
  2876. memset(s->me_cache, 0, sizeof(s->me_cache));
  2877. s->me_cache_generation += 1<<22;
  2878. //FIXME precalc
  2879. {
  2880. int x, y;
  2881. memcpy(obmc_edged, obmc_tab[s->block_max_depth], b_w*b_w*4);
  2882. if(mb_x==0)
  2883. for(y=0; y<b_w*2; y++)
  2884. memset(obmc_edged[y], obmc_edged[y][0] + obmc_edged[y][b_w-1], b_w);
  2885. if(mb_x==b_stride-1)
  2886. for(y=0; y<b_w*2; y++)
  2887. memset(obmc_edged[y]+b_w, obmc_edged[y][b_w] + obmc_edged[y][b_w*2-1], b_w);
  2888. if(mb_y==0){
  2889. for(x=0; x<b_w*2; x++)
  2890. obmc_edged[0][x] += obmc_edged[b_w-1][x];
  2891. for(y=1; y<b_w; y++)
  2892. memcpy(obmc_edged[y], obmc_edged[0], b_w*2);
  2893. }
  2894. if(mb_y==b_height-1){
  2895. for(x=0; x<b_w*2; x++)
  2896. obmc_edged[b_w*2-1][x] += obmc_edged[b_w][x];
  2897. for(y=b_w; y<b_w*2-1; y++)
  2898. memcpy(obmc_edged[y], obmc_edged[b_w*2-1], b_w*2);
  2899. }
  2900. }
  2901. //skip stuff outside the picture
  2902. if(mb_x==0 || mb_y==0 || mb_x==b_width-1 || mb_y==b_height-1)
  2903. {
  2904. uint8_t *src= s-> input_picture.data[0];
  2905. uint8_t *dst= s->current_picture.data[0];
  2906. const int stride= s->current_picture.linesize[0];
  2907. const int block_w= MB_SIZE >> s->block_max_depth;
  2908. const int sx= block_w*mb_x - block_w/2;
  2909. const int sy= block_w*mb_y - block_w/2;
  2910. const int w= s->plane[0].width;
  2911. const int h= s->plane[0].height;
  2912. int y;
  2913. for(y=sy; y<0; y++)
  2914. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2915. for(y=h; y<sy+block_w*2; y++)
  2916. memcpy(dst + sx + y*stride, src + sx + y*stride, block_w*2);
  2917. if(sx<0){
  2918. for(y=sy; y<sy+block_w*2; y++)
  2919. memcpy(dst + sx + y*stride, src + sx + y*stride, -sx);
  2920. }
  2921. if(sx+block_w*2 > w){
  2922. for(y=sy; y<sy+block_w*2; y++)
  2923. memcpy(dst + w + y*stride, src + w + y*stride, sx+block_w*2 - w);
  2924. }
  2925. }
  2926. // intra(black) = neighbors' contribution to the current block
  2927. for(i=0; i<3; i++)
  2928. color[i]= get_dc(s, mb_x, mb_y, i);
  2929. // get previous score (cant be cached due to OBMC)
  2930. if(pass > 0 && (block->type&BLOCK_INTRA)){
  2931. int color0[3]= {block->color[0], block->color[1], block->color[2]};
  2932. check_block(s, mb_x, mb_y, color0, 1, *obmc_edged, &best_rd);
  2933. }else
  2934. check_block_inter(s, mb_x, mb_y, block->mx, block->my, *obmc_edged, &best_rd);
  2935. ref_b= *block;
  2936. ref_rd= best_rd;
  2937. for(ref=0; ref < s->ref_frames; ref++){
  2938. int16_t (*mvr)[2]= &s->ref_mvs[ref][index];
  2939. if(s->ref_scores[ref][index] > s->ref_scores[ref_b.ref][index]*3/2) //FIXME tune threshold
  2940. continue;
  2941. block->ref= ref;
  2942. best_rd= INT_MAX;
  2943. check_block_inter(s, mb_x, mb_y, mvr[0][0], mvr[0][1], *obmc_edged, &best_rd);
  2944. check_block_inter(s, mb_x, mb_y, 0, 0, *obmc_edged, &best_rd);
  2945. if(tb)
  2946. check_block_inter(s, mb_x, mb_y, mvr[-b_stride][0], mvr[-b_stride][1], *obmc_edged, &best_rd);
  2947. if(lb)
  2948. check_block_inter(s, mb_x, mb_y, mvr[-1][0], mvr[-1][1], *obmc_edged, &best_rd);
  2949. if(rb)
  2950. check_block_inter(s, mb_x, mb_y, mvr[1][0], mvr[1][1], *obmc_edged, &best_rd);
  2951. if(bb)
  2952. check_block_inter(s, mb_x, mb_y, mvr[b_stride][0], mvr[b_stride][1], *obmc_edged, &best_rd);
  2953. /* fullpel ME */
  2954. //FIXME avoid subpel interpol / round to nearest integer
  2955. do{
  2956. dia_change=0;
  2957. for(i=0; i<FFMAX(s->avctx->dia_size, 1); i++){
  2958. for(j=0; j<i; j++){
  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. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+4*(i-j), block->my-(4*j), *obmc_edged, &best_rd);
  2962. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx-4*(i-j), block->my+(4*j), *obmc_edged, &best_rd);
  2963. }
  2964. }
  2965. }while(dia_change);
  2966. /* subpel ME */
  2967. do{
  2968. static const int square[8][2]= {{+1, 0},{-1, 0},{ 0,+1},{ 0,-1},{+1,+1},{-1,-1},{+1,-1},{-1,+1},};
  2969. dia_change=0;
  2970. for(i=0; i<8; i++)
  2971. dia_change |= check_block_inter(s, mb_x, mb_y, block->mx+square[i][0], block->my+square[i][1], *obmc_edged, &best_rd);
  2972. }while(dia_change);
  2973. //FIXME or try the standard 2 pass qpel or similar
  2974. mvr[0][0]= block->mx;
  2975. mvr[0][1]= block->my;
  2976. if(ref_rd > best_rd){
  2977. ref_rd= best_rd;
  2978. ref_b= *block;
  2979. }
  2980. }
  2981. best_rd= ref_rd;
  2982. *block= ref_b;
  2983. #if 1
  2984. check_block(s, mb_x, mb_y, color, 1, *obmc_edged, &best_rd);
  2985. //FIXME RD style color selection
  2986. #endif
  2987. if(!same_block(block, &backup)){
  2988. if(tb ) tb ->type &= ~BLOCK_OPT;
  2989. if(lb ) lb ->type &= ~BLOCK_OPT;
  2990. if(rb ) rb ->type &= ~BLOCK_OPT;
  2991. if(bb ) bb ->type &= ~BLOCK_OPT;
  2992. if(tlb) tlb->type &= ~BLOCK_OPT;
  2993. if(trb) trb->type &= ~BLOCK_OPT;
  2994. if(blb) blb->type &= ~BLOCK_OPT;
  2995. if(brb) brb->type &= ~BLOCK_OPT;
  2996. change ++;
  2997. }
  2998. }
  2999. }
  3000. av_log(NULL, AV_LOG_ERROR, "pass:%d changed:%d\n", pass, change);
  3001. if(!change)
  3002. break;
  3003. }
  3004. if(s->block_max_depth == 1){
  3005. int change= 0;
  3006. for(mb_y= 0; mb_y<b_height; mb_y+=2){
  3007. for(mb_x= 0; mb_x<b_width; mb_x+=2){
  3008. int i;
  3009. int best_rd, init_rd;
  3010. const int index= mb_x + mb_y * b_stride;
  3011. BlockNode *b[4];
  3012. b[0]= &s->block[index];
  3013. b[1]= b[0]+1;
  3014. b[2]= b[0]+b_stride;
  3015. b[3]= b[2]+1;
  3016. if(same_block(b[0], b[1]) &&
  3017. same_block(b[0], b[2]) &&
  3018. same_block(b[0], b[3]))
  3019. continue;
  3020. if(!s->me_cache_generation)
  3021. memset(s->me_cache, 0, sizeof(s->me_cache));
  3022. s->me_cache_generation += 1<<22;
  3023. init_rd= best_rd= get_4block_rd(s, mb_x, mb_y, 0);
  3024. //FIXME more multiref search?
  3025. check_4block_inter(s, mb_x, mb_y,
  3026. (b[0]->mx + b[1]->mx + b[2]->mx + b[3]->mx + 2) >> 2,
  3027. (b[0]->my + b[1]->my + b[2]->my + b[3]->my + 2) >> 2, 0, &best_rd);
  3028. for(i=0; i<4; i++)
  3029. if(!(b[i]->type&BLOCK_INTRA))
  3030. check_4block_inter(s, mb_x, mb_y, b[i]->mx, b[i]->my, b[i]->ref, &best_rd);
  3031. if(init_rd != best_rd)
  3032. change++;
  3033. }
  3034. }
  3035. av_log(NULL, AV_LOG_ERROR, "pass:4mv changed:%d\n", change*4);
  3036. }
  3037. }
  3038. static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
  3039. const int level= b->level;
  3040. const int w= b->width;
  3041. const int h= b->height;
  3042. const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
  3043. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3044. int x,y, thres1, thres2;
  3045. // START_TIMER
  3046. if(s->qlog == LOSSLESS_QLOG) return;
  3047. bias= bias ? 0 : (3*qmul)>>3;
  3048. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  3049. thres2= 2*thres1;
  3050. if(!bias){
  3051. for(y=0; y<h; y++){
  3052. for(x=0; x<w; x++){
  3053. int i= src[x + y*stride];
  3054. if((unsigned)(i+thres1) > thres2){
  3055. if(i>=0){
  3056. i<<= QEXPSHIFT;
  3057. i/= qmul; //FIXME optimize
  3058. src[x + y*stride]= i;
  3059. }else{
  3060. i= -i;
  3061. i<<= QEXPSHIFT;
  3062. i/= qmul; //FIXME optimize
  3063. src[x + y*stride]= -i;
  3064. }
  3065. }else
  3066. src[x + y*stride]= 0;
  3067. }
  3068. }
  3069. }else{
  3070. for(y=0; y<h; y++){
  3071. for(x=0; x<w; x++){
  3072. int i= src[x + y*stride];
  3073. if((unsigned)(i+thres1) > thres2){
  3074. if(i>=0){
  3075. i<<= QEXPSHIFT;
  3076. i= (i + bias) / qmul; //FIXME optimize
  3077. src[x + y*stride]= i;
  3078. }else{
  3079. i= -i;
  3080. i<<= QEXPSHIFT;
  3081. i= (i + bias) / qmul; //FIXME optimize
  3082. src[x + y*stride]= -i;
  3083. }
  3084. }else
  3085. src[x + y*stride]= 0;
  3086. }
  3087. }
  3088. }
  3089. if(level+1 == s->spatial_decomposition_count){
  3090. // STOP_TIMER("quantize")
  3091. }
  3092. }
  3093. static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride, int start_y, int end_y){
  3094. const int w= b->width;
  3095. const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
  3096. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3097. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  3098. int x,y;
  3099. START_TIMER
  3100. if(s->qlog == LOSSLESS_QLOG) return;
  3101. for(y=start_y; y<end_y; y++){
  3102. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  3103. DWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3104. for(x=0; x<w; x++){
  3105. int i= line[x];
  3106. if(i<0){
  3107. line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  3108. }else if(i>0){
  3109. line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
  3110. }
  3111. }
  3112. }
  3113. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  3114. STOP_TIMER("dquant")
  3115. }
  3116. }
  3117. static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
  3118. const int w= b->width;
  3119. const int h= b->height;
  3120. const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
  3121. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3122. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  3123. int x,y;
  3124. START_TIMER
  3125. if(s->qlog == LOSSLESS_QLOG) return;
  3126. for(y=0; y<h; y++){
  3127. for(x=0; x<w; x++){
  3128. int i= src[x + y*stride];
  3129. if(i<0){
  3130. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  3131. }else if(i>0){
  3132. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  3133. }
  3134. }
  3135. }
  3136. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  3137. STOP_TIMER("dquant")
  3138. }
  3139. }
  3140. static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  3141. const int w= b->width;
  3142. const int h= b->height;
  3143. int x,y;
  3144. for(y=h-1; y>=0; y--){
  3145. for(x=w-1; x>=0; x--){
  3146. int i= x + y*stride;
  3147. if(x){
  3148. if(use_median){
  3149. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3150. else src[i] -= src[i - 1];
  3151. }else{
  3152. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3153. else src[i] -= src[i - 1];
  3154. }
  3155. }else{
  3156. if(y) src[i] -= src[i - stride];
  3157. }
  3158. }
  3159. }
  3160. }
  3161. 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){
  3162. const int w= b->width;
  3163. int x,y;
  3164. // START_TIMER
  3165. DWTELEM * line;
  3166. DWTELEM * prev;
  3167. if (start_y != 0)
  3168. line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3169. for(y=start_y; y<end_y; y++){
  3170. prev = line;
  3171. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  3172. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  3173. for(x=0; x<w; x++){
  3174. if(x){
  3175. if(use_median){
  3176. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  3177. else line[x] += line[x - 1];
  3178. }else{
  3179. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  3180. else line[x] += line[x - 1];
  3181. }
  3182. }else{
  3183. if(y) line[x] += prev[x];
  3184. }
  3185. }
  3186. }
  3187. // STOP_TIMER("correlate")
  3188. }
  3189. static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  3190. const int w= b->width;
  3191. const int h= b->height;
  3192. int x,y;
  3193. for(y=0; y<h; y++){
  3194. for(x=0; x<w; x++){
  3195. int i= x + y*stride;
  3196. if(x){
  3197. if(use_median){
  3198. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  3199. else src[i] += src[i - 1];
  3200. }else{
  3201. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  3202. else src[i] += src[i - 1];
  3203. }
  3204. }else{
  3205. if(y) src[i] += src[i - stride];
  3206. }
  3207. }
  3208. }
  3209. }
  3210. static void encode_header(SnowContext *s){
  3211. int plane_index, level, orientation;
  3212. uint8_t kstate[32];
  3213. memset(kstate, MID_STATE, sizeof(kstate));
  3214. put_rac(&s->c, kstate, s->keyframe);
  3215. if(s->keyframe || s->always_reset){
  3216. reset_contexts(s);
  3217. s->last_spatial_decomposition_type=
  3218. s->last_qlog=
  3219. s->last_qbias=
  3220. s->last_mv_scale=
  3221. s->last_block_max_depth= 0;
  3222. }
  3223. if(s->keyframe){
  3224. put_symbol(&s->c, s->header_state, s->version, 0);
  3225. put_rac(&s->c, s->header_state, s->always_reset);
  3226. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  3227. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  3228. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  3229. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  3230. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  3231. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  3232. put_rac(&s->c, s->header_state, s->spatial_scalability);
  3233. // put_rac(&s->c, s->header_state, s->rate_scalability);
  3234. put_symbol(&s->c, s->header_state, s->max_ref_frames-1, 0);
  3235. for(plane_index=0; plane_index<2; plane_index++){
  3236. for(level=0; level<s->spatial_decomposition_count; level++){
  3237. for(orientation=level ? 1:0; orientation<4; orientation++){
  3238. if(orientation==2) continue;
  3239. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  3240. }
  3241. }
  3242. }
  3243. }
  3244. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type - s->last_spatial_decomposition_type, 1);
  3245. put_symbol(&s->c, s->header_state, s->qlog - s->last_qlog , 1);
  3246. put_symbol(&s->c, s->header_state, s->mv_scale - s->last_mv_scale, 1);
  3247. put_symbol(&s->c, s->header_state, s->qbias - s->last_qbias , 1);
  3248. put_symbol(&s->c, s->header_state, s->block_max_depth - s->last_block_max_depth, 1);
  3249. s->last_spatial_decomposition_type= s->spatial_decomposition_type;
  3250. s->last_qlog = s->qlog;
  3251. s->last_qbias = s->qbias;
  3252. s->last_mv_scale = s->mv_scale;
  3253. s->last_block_max_depth = s->block_max_depth;
  3254. }
  3255. static int decode_header(SnowContext *s){
  3256. int plane_index, level, orientation;
  3257. uint8_t kstate[32];
  3258. memset(kstate, MID_STATE, sizeof(kstate));
  3259. s->keyframe= get_rac(&s->c, kstate);
  3260. if(s->keyframe || s->always_reset){
  3261. reset_contexts(s);
  3262. s->spatial_decomposition_type=
  3263. s->qlog=
  3264. s->qbias=
  3265. s->mv_scale=
  3266. s->block_max_depth= 0;
  3267. }
  3268. if(s->keyframe){
  3269. s->version= get_symbol(&s->c, s->header_state, 0);
  3270. if(s->version>0){
  3271. av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
  3272. return -1;
  3273. }
  3274. s->always_reset= get_rac(&s->c, s->header_state);
  3275. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  3276. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  3277. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  3278. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  3279. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  3280. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  3281. s->spatial_scalability= get_rac(&s->c, s->header_state);
  3282. // s->rate_scalability= get_rac(&s->c, s->header_state);
  3283. s->max_ref_frames= get_symbol(&s->c, s->header_state, 0)+1;
  3284. for(plane_index=0; plane_index<3; plane_index++){
  3285. for(level=0; level<s->spatial_decomposition_count; level++){
  3286. for(orientation=level ? 1:0; orientation<4; orientation++){
  3287. int q;
  3288. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  3289. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  3290. else q= get_symbol(&s->c, s->header_state, 1);
  3291. s->plane[plane_index].band[level][orientation].qlog= q;
  3292. }
  3293. }
  3294. }
  3295. }
  3296. s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
  3297. if(s->spatial_decomposition_type > 2){
  3298. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  3299. return -1;
  3300. }
  3301. s->qlog += get_symbol(&s->c, s->header_state, 1);
  3302. s->mv_scale += get_symbol(&s->c, s->header_state, 1);
  3303. s->qbias += get_symbol(&s->c, s->header_state, 1);
  3304. s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
  3305. if(s->block_max_depth > 1 || s->block_max_depth < 0){
  3306. av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
  3307. s->block_max_depth= 0;
  3308. return -1;
  3309. }
  3310. return 0;
  3311. }
  3312. static void init_qexp(void){
  3313. int i;
  3314. double v=128;
  3315. for(i=0; i<QROOT; i++){
  3316. qexp[i]= lrintf(v);
  3317. v *= pow(2, 1.0 / QROOT);
  3318. }
  3319. }
  3320. static int common_init(AVCodecContext *avctx){
  3321. SnowContext *s = avctx->priv_data;
  3322. int width, height;
  3323. int level, orientation, plane_index, dec;
  3324. int i, j;
  3325. s->avctx= avctx;
  3326. dsputil_init(&s->dsp, avctx);
  3327. #define mcf(dx,dy)\
  3328. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  3329. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  3330. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  3331. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  3332. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  3333. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  3334. mcf( 0, 0)
  3335. mcf( 4, 0)
  3336. mcf( 8, 0)
  3337. mcf(12, 0)
  3338. mcf( 0, 4)
  3339. mcf( 4, 4)
  3340. mcf( 8, 4)
  3341. mcf(12, 4)
  3342. mcf( 0, 8)
  3343. mcf( 4, 8)
  3344. mcf( 8, 8)
  3345. mcf(12, 8)
  3346. mcf( 0,12)
  3347. mcf( 4,12)
  3348. mcf( 8,12)
  3349. mcf(12,12)
  3350. #define mcfh(dx,dy)\
  3351. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  3352. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  3353. mc_block_hpel ## dx ## dy ## 16;\
  3354. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  3355. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  3356. mc_block_hpel ## dx ## dy ## 8;
  3357. mcfh(0, 0)
  3358. mcfh(8, 0)
  3359. mcfh(0, 8)
  3360. mcfh(8, 8)
  3361. if(!qexp[0])
  3362. init_qexp();
  3363. dec= s->spatial_decomposition_count= 5;
  3364. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  3365. s->chroma_h_shift= 1; //FIXME XXX
  3366. s->chroma_v_shift= 1;
  3367. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  3368. width= s->avctx->width;
  3369. height= s->avctx->height;
  3370. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
  3371. s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  3372. s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
  3373. for(plane_index=0; plane_index<3; plane_index++){
  3374. int w= s->avctx->width;
  3375. int h= s->avctx->height;
  3376. if(plane_index){
  3377. w>>= s->chroma_h_shift;
  3378. h>>= s->chroma_v_shift;
  3379. }
  3380. s->plane[plane_index].width = w;
  3381. s->plane[plane_index].height= h;
  3382. //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
  3383. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  3384. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3385. SubBand *b= &s->plane[plane_index].band[level][orientation];
  3386. b->buf= s->spatial_dwt_buffer;
  3387. b->level= level;
  3388. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  3389. b->width = (w + !(orientation&1))>>1;
  3390. b->height= (h + !(orientation>1))>>1;
  3391. b->stride_line = 1 << (s->spatial_decomposition_count - level);
  3392. b->buf_x_offset = 0;
  3393. b->buf_y_offset = 0;
  3394. if(orientation&1){
  3395. b->buf += (w+1)>>1;
  3396. b->buf_x_offset = (w+1)>>1;
  3397. }
  3398. if(orientation>1){
  3399. b->buf += b->stride>>1;
  3400. b->buf_y_offset = b->stride_line >> 1;
  3401. }
  3402. if(level)
  3403. b->parent= &s->plane[plane_index].band[level-1][orientation];
  3404. b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
  3405. }
  3406. w= (w+1)>>1;
  3407. h= (h+1)>>1;
  3408. }
  3409. }
  3410. for(i=0; i<MAX_REF_FRAMES; i++)
  3411. for(j=0; j<MAX_REF_FRAMES; j++)
  3412. scale_mv_ref[i][j] = 256*(i+1)/(j+1);
  3413. reset_contexts(s);
  3414. /*
  3415. width= s->width= avctx->width;
  3416. height= s->height= avctx->height;
  3417. assert(width && height);
  3418. */
  3419. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  3420. return 0;
  3421. }
  3422. static int qscale2qlog(int qscale){
  3423. return rint(QROOT*log(qscale / (float)FF_QP2LAMBDA)/log(2))
  3424. + 61*QROOT/8; //<64 >60
  3425. }
  3426. static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
  3427. {
  3428. /* estimate the frame's complexity as a sum of weighted dwt coefs.
  3429. * FIXME we know exact mv bits at this point,
  3430. * but ratecontrol isn't set up to include them. */
  3431. uint32_t coef_sum= 0;
  3432. int level, orientation, delta_qlog;
  3433. for(level=0; level<s->spatial_decomposition_count; level++){
  3434. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3435. SubBand *b= &s->plane[0].band[level][orientation];
  3436. DWTELEM *buf= b->buf;
  3437. const int w= b->width;
  3438. const int h= b->height;
  3439. const int stride= b->stride;
  3440. const int qlog= clip(2*QROOT + b->qlog, 0, QROOT*16);
  3441. const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
  3442. const int qdiv= (1<<16)/qmul;
  3443. int x, y;
  3444. if(orientation==0)
  3445. decorrelate(s, b, buf, stride, 1, 0);
  3446. for(y=0; y<h; y++)
  3447. for(x=0; x<w; x++)
  3448. coef_sum+= abs(buf[x+y*stride]) * qdiv >> 16;
  3449. if(orientation==0)
  3450. correlate(s, b, buf, stride, 1, 0);
  3451. }
  3452. }
  3453. /* ugly, ratecontrol just takes a sqrt again */
  3454. coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
  3455. assert(coef_sum < INT_MAX);
  3456. if(pict->pict_type == I_TYPE){
  3457. s->m.current_picture.mb_var_sum= coef_sum;
  3458. s->m.current_picture.mc_mb_var_sum= 0;
  3459. }else{
  3460. s->m.current_picture.mc_mb_var_sum= coef_sum;
  3461. s->m.current_picture.mb_var_sum= 0;
  3462. }
  3463. pict->quality= ff_rate_estimate_qscale(&s->m, 1);
  3464. if (pict->quality < 0)
  3465. return INT_MIN;
  3466. s->lambda= pict->quality * 3/2;
  3467. delta_qlog= qscale2qlog(pict->quality) - s->qlog;
  3468. s->qlog+= delta_qlog;
  3469. return delta_qlog;
  3470. }
  3471. static void calculate_vissual_weight(SnowContext *s, Plane *p){
  3472. int width = p->width;
  3473. int height= p->height;
  3474. int level, orientation, x, y;
  3475. for(level=0; level<s->spatial_decomposition_count; level++){
  3476. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3477. SubBand *b= &p->band[level][orientation];
  3478. DWTELEM *buf= b->buf;
  3479. int64_t error=0;
  3480. memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
  3481. buf[b->width/2 + b->height/2*b->stride]= 256*256;
  3482. ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3483. for(y=0; y<height; y++){
  3484. for(x=0; x<width; x++){
  3485. int64_t d= s->spatial_dwt_buffer[x + y*width];
  3486. error += d*d;
  3487. }
  3488. }
  3489. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  3490. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
  3491. }
  3492. }
  3493. }
  3494. static int encode_init(AVCodecContext *avctx)
  3495. {
  3496. SnowContext *s = avctx->priv_data;
  3497. int plane_index;
  3498. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  3499. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it may not be decodable with future versions!!!\n"
  3500. "use vstrict=-2 / -strict -2 to use it anyway\n");
  3501. return -1;
  3502. }
  3503. if(avctx->prediction_method == DWT_97
  3504. && (avctx->flags & CODEC_FLAG_QSCALE)
  3505. && avctx->global_quality == 0){
  3506. av_log(avctx, AV_LOG_ERROR, "the 9/7 wavelet is incompatible with lossless mode\n");
  3507. return -1;
  3508. }
  3509. common_init(avctx);
  3510. alloc_blocks(s);
  3511. s->version=0;
  3512. s->m.avctx = avctx;
  3513. s->m.flags = avctx->flags;
  3514. s->m.bit_rate= avctx->bit_rate;
  3515. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  3516. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  3517. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  3518. s->m.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
  3519. h263_encode_init(&s->m); //mv_penalty
  3520. s->max_ref_frames = FFMAX(FFMIN(avctx->refs, MAX_REF_FRAMES), 1);
  3521. if(avctx->flags&CODEC_FLAG_PASS1){
  3522. if(!avctx->stats_out)
  3523. avctx->stats_out = av_mallocz(256);
  3524. }
  3525. if((avctx->flags&CODEC_FLAG_PASS2) || !(avctx->flags&CODEC_FLAG_QSCALE)){
  3526. if(ff_rate_control_init(&s->m) < 0)
  3527. return -1;
  3528. }
  3529. s->pass1_rc= !(avctx->flags & (CODEC_FLAG_QSCALE|CODEC_FLAG_PASS2));
  3530. for(plane_index=0; plane_index<3; plane_index++){
  3531. calculate_vissual_weight(s, &s->plane[plane_index]);
  3532. }
  3533. avctx->coded_frame= &s->current_picture;
  3534. switch(avctx->pix_fmt){
  3535. // case PIX_FMT_YUV444P:
  3536. // case PIX_FMT_YUV422P:
  3537. case PIX_FMT_YUV420P:
  3538. case PIX_FMT_GRAY8:
  3539. // case PIX_FMT_YUV411P:
  3540. // case PIX_FMT_YUV410P:
  3541. s->colorspace_type= 0;
  3542. break;
  3543. /* case PIX_FMT_RGBA32:
  3544. s->colorspace= 1;
  3545. break;*/
  3546. default:
  3547. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  3548. return -1;
  3549. }
  3550. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  3551. s->chroma_h_shift= 1;
  3552. s->chroma_v_shift= 1;
  3553. ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
  3554. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  3555. s->avctx->get_buffer(s->avctx, &s->input_picture);
  3556. if(s->avctx->me_method == ME_ITER){
  3557. int i;
  3558. int size= s->b_width * s->b_height << 2*s->block_max_depth;
  3559. for(i=0; i<s->max_ref_frames; i++){
  3560. s->ref_mvs[i]= av_mallocz(size*sizeof(int16_t[2]));
  3561. s->ref_scores[i]= av_mallocz(size*sizeof(uint32_t));
  3562. }
  3563. }
  3564. return 0;
  3565. }
  3566. static int frame_start(SnowContext *s){
  3567. AVFrame tmp;
  3568. int w= s->avctx->width; //FIXME round up to x16 ?
  3569. int h= s->avctx->height;
  3570. if(s->current_picture.data[0]){
  3571. draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  3572. draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  3573. draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  3574. }
  3575. tmp= s->last_picture[s->max_ref_frames-1];
  3576. memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
  3577. s->last_picture[0]= s->current_picture;
  3578. s->current_picture= tmp;
  3579. if(s->keyframe){
  3580. s->ref_frames= 0;
  3581. }else{
  3582. int i;
  3583. for(i=0; i<s->max_ref_frames && s->last_picture[i].data[0]; i++)
  3584. if(i && s->last_picture[i-1].key_frame)
  3585. break;
  3586. s->ref_frames= i;
  3587. }
  3588. s->current_picture.reference= 1;
  3589. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  3590. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  3591. return -1;
  3592. }
  3593. s->current_picture.key_frame= s->keyframe;
  3594. return 0;
  3595. }
  3596. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  3597. SnowContext *s = avctx->priv_data;
  3598. RangeCoder * const c= &s->c;
  3599. AVFrame *pict = data;
  3600. const int width= s->avctx->width;
  3601. const int height= s->avctx->height;
  3602. int level, orientation, plane_index, i, y;
  3603. uint8_t rc_header_bak[sizeof(s->header_state)];
  3604. uint8_t rc_block_bak[sizeof(s->block_state)];
  3605. ff_init_range_encoder(c, buf, buf_size);
  3606. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3607. for(i=0; i<3; i++){
  3608. int shift= !!i;
  3609. for(y=0; y<(height>>shift); y++)
  3610. memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
  3611. &pict->data[i][y * pict->linesize[i]],
  3612. width>>shift);
  3613. }
  3614. s->new_picture = *pict;
  3615. s->m.picture_number= avctx->frame_number;
  3616. if(avctx->flags&CODEC_FLAG_PASS2){
  3617. s->m.pict_type =
  3618. pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
  3619. s->keyframe= pict->pict_type==FF_I_TYPE;
  3620. if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
  3621. pict->quality= ff_rate_estimate_qscale(&s->m, 0);
  3622. if (pict->quality < 0)
  3623. return -1;
  3624. }
  3625. }else{
  3626. s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  3627. s->m.pict_type=
  3628. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  3629. }
  3630. if(s->pass1_rc && avctx->frame_number == 0)
  3631. pict->quality= 2*FF_QP2LAMBDA;
  3632. if(pict->quality){
  3633. s->qlog= qscale2qlog(pict->quality);
  3634. s->lambda = pict->quality * 3/2;
  3635. }
  3636. if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
  3637. s->qlog= LOSSLESS_QLOG;
  3638. s->lambda = 0;
  3639. }//else keep previous frame's qlog until after motion est
  3640. frame_start(s);
  3641. s->m.current_picture_ptr= &s->m.current_picture;
  3642. if(pict->pict_type == P_TYPE){
  3643. int block_width = (width +15)>>4;
  3644. int block_height= (height+15)>>4;
  3645. int stride= s->current_picture.linesize[0];
  3646. assert(s->current_picture.data[0]);
  3647. assert(s->last_picture[0].data[0]);
  3648. s->m.avctx= s->avctx;
  3649. s->m.current_picture.data[0]= s->current_picture.data[0];
  3650. s->m. last_picture.data[0]= s->last_picture[0].data[0];
  3651. s->m. new_picture.data[0]= s-> input_picture.data[0];
  3652. s->m. last_picture_ptr= &s->m. last_picture;
  3653. s->m.linesize=
  3654. s->m. last_picture.linesize[0]=
  3655. s->m. new_picture.linesize[0]=
  3656. s->m.current_picture.linesize[0]= stride;
  3657. s->m.uvlinesize= s->current_picture.linesize[1];
  3658. s->m.width = width;
  3659. s->m.height= height;
  3660. s->m.mb_width = block_width;
  3661. s->m.mb_height= block_height;
  3662. s->m.mb_stride= s->m.mb_width+1;
  3663. s->m.b8_stride= 2*s->m.mb_width+1;
  3664. s->m.f_code=1;
  3665. s->m.pict_type= pict->pict_type;
  3666. s->m.me_method= s->avctx->me_method;
  3667. s->m.me.scene_change_score=0;
  3668. s->m.flags= s->avctx->flags;
  3669. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  3670. s->m.out_format= FMT_H263;
  3671. s->m.unrestricted_mv= 1;
  3672. s->m.lambda = s->lambda;
  3673. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  3674. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  3675. s->m.dsp= s->dsp; //move
  3676. ff_init_me(&s->m);
  3677. s->dsp= s->m.dsp;
  3678. }
  3679. if(s->pass1_rc){
  3680. memcpy(rc_header_bak, s->header_state, sizeof(s->header_state));
  3681. memcpy(rc_block_bak, s->block_state, sizeof(s->block_state));
  3682. }
  3683. redo_frame:
  3684. s->m.pict_type = pict->pict_type;
  3685. s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
  3686. encode_header(s);
  3687. s->m.misc_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3688. encode_blocks(s, 1);
  3689. s->m.mv_bits = 8*(s->c.bytestream - s->c.bytestream_start) - s->m.misc_bits;
  3690. for(plane_index=0; plane_index<3; plane_index++){
  3691. Plane *p= &s->plane[plane_index];
  3692. int w= p->width;
  3693. int h= p->height;
  3694. int x, y;
  3695. // int bits= put_bits_count(&s->c.pb);
  3696. if(!(avctx->flags2 & CODEC_FLAG2_MEMC_ONLY)){
  3697. //FIXME optimize
  3698. if(pict->data[plane_index]) //FIXME gray hack
  3699. for(y=0; y<h; y++){
  3700. for(x=0; x<w; x++){
  3701. s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3702. }
  3703. }
  3704. predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
  3705. if( plane_index==0
  3706. && pict->pict_type == P_TYPE
  3707. && !(avctx->flags&CODEC_FLAG_PASS2)
  3708. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3709. ff_init_range_encoder(c, buf, buf_size);
  3710. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3711. pict->pict_type= FF_I_TYPE;
  3712. s->keyframe=1;
  3713. s->current_picture.key_frame=1;
  3714. goto redo_frame;
  3715. }
  3716. if(s->qlog == LOSSLESS_QLOG){
  3717. for(y=0; y<h; y++){
  3718. for(x=0; x<w; x++){
  3719. s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1))-1)>>FRAC_BITS;
  3720. }
  3721. }
  3722. }
  3723. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3724. if(s->pass1_rc && plane_index==0){
  3725. int delta_qlog = ratecontrol_1pass(s, pict);
  3726. if (delta_qlog <= INT_MIN)
  3727. return -1;
  3728. if(delta_qlog){
  3729. //reordering qlog in the bitstream would eliminate this reset
  3730. ff_init_range_encoder(c, buf, buf_size);
  3731. memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
  3732. memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
  3733. encode_header(s);
  3734. encode_blocks(s, 0);
  3735. }
  3736. }
  3737. for(level=0; level<s->spatial_decomposition_count; level++){
  3738. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3739. SubBand *b= &p->band[level][orientation];
  3740. quantize(s, b, b->buf, b->stride, s->qbias);
  3741. if(orientation==0)
  3742. decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
  3743. encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  3744. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3745. if(orientation==0)
  3746. correlate(s, b, b->buf, b->stride, 1, 0);
  3747. }
  3748. }
  3749. // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
  3750. for(level=0; level<s->spatial_decomposition_count; level++){
  3751. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3752. SubBand *b= &p->band[level][orientation];
  3753. dequantize(s, b, b->buf, b->stride);
  3754. }
  3755. }
  3756. ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3757. if(s->qlog == LOSSLESS_QLOG){
  3758. for(y=0; y<h; y++){
  3759. for(x=0; x<w; x++){
  3760. s->spatial_dwt_buffer[y*w + x]<<=FRAC_BITS;
  3761. }
  3762. }
  3763. }
  3764. {START_TIMER
  3765. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3766. STOP_TIMER("pred-conv")}
  3767. }else{
  3768. //ME/MC only
  3769. if(pict->pict_type == I_TYPE){
  3770. for(y=0; y<h; y++){
  3771. for(x=0; x<w; x++){
  3772. s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
  3773. pict->data[plane_index][y*pict->linesize[plane_index] + x];
  3774. }
  3775. }
  3776. }else{
  3777. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  3778. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3779. }
  3780. }
  3781. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3782. int64_t error= 0;
  3783. if(pict->data[plane_index]) //FIXME gray hack
  3784. for(y=0; y<h; y++){
  3785. for(x=0; x<w; x++){
  3786. 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];
  3787. error += d*d;
  3788. }
  3789. }
  3790. s->avctx->error[plane_index] += error;
  3791. s->current_picture.error[plane_index] = error;
  3792. }
  3793. }
  3794. if(s->last_picture[s->max_ref_frames-1].data[0])
  3795. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  3796. s->current_picture.coded_picture_number = avctx->frame_number;
  3797. s->current_picture.pict_type = pict->pict_type;
  3798. s->current_picture.quality = pict->quality;
  3799. s->m.frame_bits = 8*(s->c.bytestream - s->c.bytestream_start);
  3800. s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
  3801. s->m.current_picture.display_picture_number =
  3802. s->m.current_picture.coded_picture_number = avctx->frame_number;
  3803. s->m.current_picture.quality = pict->quality;
  3804. s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
  3805. if(s->pass1_rc)
  3806. if (ff_rate_estimate_qscale(&s->m, 0) < 0)
  3807. return -1;
  3808. if(avctx->flags&CODEC_FLAG_PASS1)
  3809. ff_write_pass1_stats(&s->m);
  3810. s->m.last_pict_type = s->m.pict_type;
  3811. avctx->frame_bits = s->m.frame_bits;
  3812. avctx->mv_bits = s->m.mv_bits;
  3813. avctx->misc_bits = s->m.misc_bits;
  3814. avctx->p_tex_bits = s->m.p_tex_bits;
  3815. emms_c();
  3816. return ff_rac_terminate(c);
  3817. }
  3818. static void common_end(SnowContext *s){
  3819. int plane_index, level, orientation, i;
  3820. av_freep(&s->spatial_dwt_buffer);
  3821. av_freep(&s->m.me.scratchpad);
  3822. av_freep(&s->m.me.map);
  3823. av_freep(&s->m.me.score_map);
  3824. av_freep(&s->m.obmc_scratchpad);
  3825. av_freep(&s->block);
  3826. for(i=0; i<MAX_REF_FRAMES; i++){
  3827. av_freep(&s->ref_mvs[i]);
  3828. av_freep(&s->ref_scores[i]);
  3829. if(s->last_picture[i].data[0])
  3830. s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
  3831. }
  3832. for(plane_index=0; plane_index<3; plane_index++){
  3833. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  3834. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3835. SubBand *b= &s->plane[plane_index].band[level][orientation];
  3836. av_freep(&b->x_coeff);
  3837. }
  3838. }
  3839. }
  3840. }
  3841. static int encode_end(AVCodecContext *avctx)
  3842. {
  3843. SnowContext *s = avctx->priv_data;
  3844. common_end(s);
  3845. av_free(avctx->stats_out);
  3846. return 0;
  3847. }
  3848. static int decode_init(AVCodecContext *avctx)
  3849. {
  3850. SnowContext *s = avctx->priv_data;
  3851. int block_size;
  3852. avctx->pix_fmt= PIX_FMT_YUV420P;
  3853. common_init(avctx);
  3854. block_size = MB_SIZE >> s->block_max_depth;
  3855. 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);
  3856. return 0;
  3857. }
  3858. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  3859. SnowContext *s = avctx->priv_data;
  3860. RangeCoder * const c= &s->c;
  3861. int bytes_read;
  3862. AVFrame *picture = data;
  3863. int level, orientation, plane_index;
  3864. ff_init_range_decoder(c, buf, buf_size);
  3865. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3866. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  3867. decode_header(s);
  3868. if(!s->block) alloc_blocks(s);
  3869. frame_start(s);
  3870. //keyframe flag dupliaction mess FIXME
  3871. if(avctx->debug&FF_DEBUG_PICT_INFO)
  3872. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  3873. decode_blocks(s);
  3874. for(plane_index=0; plane_index<3; plane_index++){
  3875. Plane *p= &s->plane[plane_index];
  3876. int w= p->width;
  3877. int h= p->height;
  3878. int x, y;
  3879. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  3880. if(s->avctx->debug&2048){
  3881. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  3882. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3883. for(y=0; y<h; y++){
  3884. for(x=0; x<w; x++){
  3885. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  3886. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  3887. }
  3888. }
  3889. }
  3890. { START_TIMER
  3891. for(level=0; level<s->spatial_decomposition_count; level++){
  3892. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3893. SubBand *b= &p->band[level][orientation];
  3894. unpack_coeffs(s, b, b->parent, orientation);
  3895. }
  3896. }
  3897. STOP_TIMER("unpack coeffs");
  3898. }
  3899. {START_TIMER
  3900. const int mb_h= s->b_height << s->block_max_depth;
  3901. const int block_size = MB_SIZE >> s->block_max_depth;
  3902. const int block_w = plane_index ? block_size/2 : block_size;
  3903. int mb_y;
  3904. dwt_compose_t cs[MAX_DECOMPOSITIONS];
  3905. int yd=0, yq=0;
  3906. int y;
  3907. int end_y;
  3908. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3909. for(mb_y=0; mb_y<=mb_h; mb_y++){
  3910. int slice_starty = block_w*mb_y;
  3911. int slice_h = block_w*(mb_y+1);
  3912. if (!(s->keyframe || s->avctx->debug&512)){
  3913. slice_starty = FFMAX(0, slice_starty - (block_w >> 1));
  3914. slice_h -= (block_w >> 1);
  3915. }
  3916. {
  3917. START_TIMER
  3918. for(level=0; level<s->spatial_decomposition_count; level++){
  3919. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3920. SubBand *b= &p->band[level][orientation];
  3921. int start_y;
  3922. int end_y;
  3923. int our_mb_start = mb_y;
  3924. int our_mb_end = (mb_y + 1);
  3925. const int extra= 3;
  3926. start_y = (mb_y ? ((block_w * our_mb_start) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra: 0);
  3927. end_y = (((block_w * our_mb_end) >> (s->spatial_decomposition_count - level)) + s->spatial_decomposition_count - level + extra);
  3928. if (!(s->keyframe || s->avctx->debug&512)){
  3929. start_y = FFMAX(0, start_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  3930. end_y = FFMAX(0, end_y - (block_w >> (1+s->spatial_decomposition_count - level)));
  3931. }
  3932. start_y = FFMIN(b->height, start_y);
  3933. end_y = FFMIN(b->height, end_y);
  3934. if (start_y != end_y){
  3935. if (orientation == 0){
  3936. SubBand * correlate_band = &p->band[0][0];
  3937. int correlate_end_y = FFMIN(b->height, end_y + 1);
  3938. int correlate_start_y = FFMIN(b->height, (start_y ? start_y + 1 : 0));
  3939. decode_subband_slice_buffered(s, correlate_band, &s->sb, correlate_start_y, correlate_end_y, decode_state[0][0]);
  3940. correlate_slice_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, 1, 0, correlate_start_y, correlate_end_y);
  3941. dequantize_slice_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, start_y, end_y);
  3942. }
  3943. else
  3944. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  3945. }
  3946. }
  3947. }
  3948. STOP_TIMER("decode_subband_slice");
  3949. }
  3950. { START_TIMER
  3951. for(; yd<slice_h; yd+=4){
  3952. ff_spatial_idwt_buffered_slice(&s->dsp, cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  3953. }
  3954. STOP_TIMER("idwt slice");}
  3955. if(s->qlog == LOSSLESS_QLOG){
  3956. for(; yq<slice_h && yq<h; yq++){
  3957. DWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  3958. for(x=0; x<w; x++){
  3959. line[x] <<= FRAC_BITS;
  3960. }
  3961. }
  3962. }
  3963. predict_slice_buffered(s, &s->sb, s->spatial_dwt_buffer, plane_index, 1, mb_y);
  3964. y = FFMIN(p->height, slice_starty);
  3965. end_y = FFMIN(p->height, slice_h);
  3966. while(y < end_y)
  3967. slice_buffer_release(&s->sb, y++);
  3968. }
  3969. slice_buffer_flush(&s->sb);
  3970. STOP_TIMER("idwt + predict_slices")}
  3971. }
  3972. emms_c();
  3973. if(s->last_picture[s->max_ref_frames-1].data[0])
  3974. avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
  3975. if(!(s->avctx->debug&2048))
  3976. *picture= s->current_picture;
  3977. else
  3978. *picture= s->mconly_picture;
  3979. *data_size = sizeof(AVFrame);
  3980. bytes_read= c->bytestream - c->bytestream_start;
  3981. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  3982. return bytes_read;
  3983. }
  3984. static int decode_end(AVCodecContext *avctx)
  3985. {
  3986. SnowContext *s = avctx->priv_data;
  3987. slice_buffer_destroy(&s->sb);
  3988. common_end(s);
  3989. return 0;
  3990. }
  3991. AVCodec snow_decoder = {
  3992. "snow",
  3993. CODEC_TYPE_VIDEO,
  3994. CODEC_ID_SNOW,
  3995. sizeof(SnowContext),
  3996. decode_init,
  3997. NULL,
  3998. decode_end,
  3999. decode_frame,
  4000. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  4001. NULL
  4002. };
  4003. #ifdef CONFIG_ENCODERS
  4004. AVCodec snow_encoder = {
  4005. "snow",
  4006. CODEC_TYPE_VIDEO,
  4007. CODEC_ID_SNOW,
  4008. sizeof(SnowContext),
  4009. encode_init,
  4010. encode_frame,
  4011. encode_end,
  4012. };
  4013. #endif
  4014. #if 0
  4015. #undef malloc
  4016. #undef free
  4017. #undef printf
  4018. int main(){
  4019. int width=256;
  4020. int height=256;
  4021. int buffer[2][width*height];
  4022. SnowContext s;
  4023. int i;
  4024. s.spatial_decomposition_count=6;
  4025. s.spatial_decomposition_type=1;
  4026. printf("testing 5/3 DWT\n");
  4027. for(i=0; i<width*height; i++)
  4028. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  4029. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4030. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4031. for(i=0; i<width*height; i++)
  4032. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  4033. printf("testing 9/7 DWT\n");
  4034. s.spatial_decomposition_type=0;
  4035. for(i=0; i<width*height; i++)
  4036. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  4037. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4038. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4039. for(i=0; i<width*height; i++)
  4040. if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  4041. #if 0
  4042. printf("testing AC coder\n");
  4043. memset(s.header_state, 0, sizeof(s.header_state));
  4044. ff_init_range_encoder(&s.c, buffer[0], 256*256);
  4045. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  4046. for(i=-256; i<256; i++){
  4047. START_TIMER
  4048. put_symbol(&s.c, s.header_state, i*i*i/3*FFABS(i), 1);
  4049. STOP_TIMER("put_symbol")
  4050. }
  4051. ff_rac_terminate(&s.c);
  4052. memset(s.header_state, 0, sizeof(s.header_state));
  4053. ff_init_range_decoder(&s.c, buffer[0], 256*256);
  4054. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  4055. for(i=-256; i<256; i++){
  4056. int j;
  4057. START_TIMER
  4058. j= get_symbol(&s.c, s.header_state, 1);
  4059. STOP_TIMER("get_symbol")
  4060. if(j!=i*i*i/3*FFABS(i)) printf("fsck: %d != %d\n", i, j);
  4061. }
  4062. #endif
  4063. {
  4064. int level, orientation, x, y;
  4065. int64_t errors[8][4];
  4066. int64_t g=0;
  4067. memset(errors, 0, sizeof(errors));
  4068. s.spatial_decomposition_count=3;
  4069. s.spatial_decomposition_type=0;
  4070. for(level=0; level<s.spatial_decomposition_count; level++){
  4071. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  4072. int w= width >> (s.spatial_decomposition_count-level);
  4073. int h= height >> (s.spatial_decomposition_count-level);
  4074. int stride= width << (s.spatial_decomposition_count-level);
  4075. DWTELEM *buf= buffer[0];
  4076. int64_t error=0;
  4077. if(orientation&1) buf+=w;
  4078. if(orientation>1) buf+=stride>>1;
  4079. memset(buffer[0], 0, sizeof(int)*width*height);
  4080. buf[w/2 + h/2*stride]= 256*256;
  4081. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4082. for(y=0; y<height; y++){
  4083. for(x=0; x<width; x++){
  4084. int64_t d= buffer[0][x + y*width];
  4085. error += d*d;
  4086. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
  4087. }
  4088. if(FFABS(height/2-y)<9 && level==2) printf("\n");
  4089. }
  4090. error= (int)(sqrt(error)+0.5);
  4091. errors[level][orientation]= error;
  4092. if(g) g=ff_gcd(g, error);
  4093. else g= error;
  4094. }
  4095. }
  4096. printf("static int const visual_weight[][4]={\n");
  4097. for(level=0; level<s.spatial_decomposition_count; level++){
  4098. printf(" {");
  4099. for(orientation=0; orientation<4; orientation++){
  4100. printf("%8"PRId64",", errors[level][orientation]/g);
  4101. }
  4102. printf("},\n");
  4103. }
  4104. printf("};\n");
  4105. {
  4106. int level=2;
  4107. int orientation=3;
  4108. int w= width >> (s.spatial_decomposition_count-level);
  4109. int h= height >> (s.spatial_decomposition_count-level);
  4110. int stride= width << (s.spatial_decomposition_count-level);
  4111. DWTELEM *buf= buffer[0];
  4112. int64_t error=0;
  4113. buf+=w;
  4114. buf+=stride>>1;
  4115. memset(buffer[0], 0, sizeof(int)*width*height);
  4116. #if 1
  4117. for(y=0; y<height; y++){
  4118. for(x=0; x<width; x++){
  4119. int tab[4]={0,2,3,1};
  4120. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  4121. }
  4122. }
  4123. ff_spatial_dwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4124. #else
  4125. for(y=0; y<h; y++){
  4126. for(x=0; x<w; x++){
  4127. buf[x + y*stride ]=169;
  4128. buf[x + y*stride-w]=64;
  4129. }
  4130. }
  4131. ff_spatial_idwt(buffer[0], width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
  4132. #endif
  4133. for(y=0; y<height; y++){
  4134. for(x=0; x<width; x++){
  4135. int64_t d= buffer[0][x + y*width];
  4136. error += d*d;
  4137. if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
  4138. }
  4139. if(FFABS(height/2-y)<9) printf("\n");
  4140. }
  4141. }
  4142. }
  4143. return 0;
  4144. }
  4145. #endif