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.

4015 lines
135KB

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