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.

4622 lines
159KB

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