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.

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