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.

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