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.

4631 lines
160KB

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