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.

4381 lines
150KB

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