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.

3146 lines
105KB

  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 "cabac.h"
  22. #include "mpegvideo.h"
  23. #undef NDEBUG
  24. #include <assert.h>
  25. #define MAX_DECOMPOSITIONS 8
  26. #define MAX_PLANES 4
  27. #define DWTELEM int
  28. #define QROOT 8
  29. #define LOSSLESS_QLOG -128
  30. #define FRAC_BITS 8
  31. static const int8_t quant3[256]={
  32. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  33. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  34. 1, 1, 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, 0,
  48. };
  49. static const int8_t quant3b[256]={
  50. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  51. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  52. 1, 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. };
  67. static const int8_t quant5[256]={
  68. 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  69. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  70. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  71. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  72. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  73. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  74. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  75. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  76. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  77. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  78. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  79. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  80. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  81. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  82. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  83. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
  84. };
  85. static const int8_t quant7[256]={
  86. 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  87. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  88. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  89. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  90. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  91. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  92. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  93. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  94. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  95. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  96. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  97. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  98. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
  99. -3,-3,-3,-3,-3,-3,-3,-3,-3,-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,-1,-1,
  102. };
  103. static const int8_t quant9[256]={
  104. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  105. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  106. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  107. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  108. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  109. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  110. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  111. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  112. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  113. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  114. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  115. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  116. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  117. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  118. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  119. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  120. };
  121. static const int8_t quant11[256]={
  122. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  123. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  124. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  125. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  126. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  127. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  128. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  129. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  130. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  131. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  132. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  133. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  134. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  135. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-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,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
  138. };
  139. static const int8_t quant13[256]={
  140. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  141. 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  142. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  143. 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  144. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  145. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  146. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  147. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  148. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  149. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  150. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  151. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  152. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-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. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  156. };
  157. #define LOG2_OBMC_MAX 6
  158. #define OBMC_MAX (1<<(LOG2_OBMC_MAX))
  159. #if 0 //64*cubic
  160. static const uint8_t obmc32[1024]={
  161. 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,
  162. 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,
  163. 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,
  164. 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,
  165. 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,
  166. 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,
  167. 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,
  168. 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,
  169. 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,
  170. 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,
  171. 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,
  172. 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,
  173. 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,
  174. 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,
  175. 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,
  176. 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,
  177. 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,
  178. 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,
  179. 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,
  180. 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,
  181. 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,
  182. 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,
  183. 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,
  184. 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,
  185. 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,
  186. 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,
  187. 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
  188. 0, 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,
  189. 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,
  190. 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,
  191. 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,
  192. 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,
  193. //error:0.000022
  194. };
  195. static const uint8_t obmc16[256]={
  196. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  197. 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
  198. 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
  199. 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
  200. 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
  201. 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
  202. 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
  203. 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
  204. 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
  205. 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
  206. 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
  207. 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
  208. 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
  209. 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
  210. 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
  211. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  212. //error:0.000033
  213. };
  214. #elif 1 // 64*linear
  215. static const uint8_t obmc32[1024]={
  216. 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,
  217. 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,
  218. 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,
  219. 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,
  220. 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,
  221. 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,
  222. 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,
  223. 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,
  224. 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,
  225. 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,
  226. 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,
  227. 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,
  228. 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,
  229. 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,
  230. 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,
  231. 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,
  232. 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,
  233. 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,
  234. 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,
  235. 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,
  236. 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,
  237. 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,
  238. 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,
  239. 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,
  240. 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,
  241. 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,
  242. 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
  243. 1, 2, 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,
  244. 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,
  245. 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,
  246. 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,
  247. 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,
  248. //error:0.000020
  249. };
  250. static const uint8_t obmc16[256]={
  251. 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
  252. 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
  253. 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
  254. 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
  255. 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
  256. 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
  257. 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
  258. 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
  259. 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
  260. 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
  261. 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
  262. 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
  263. 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
  264. 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
  265. 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
  266. 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
  267. //error:0.000015
  268. };
  269. #else //64*cos
  270. static const uint8_t obmc32[1024]={
  271. 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,
  272. 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,
  273. 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,
  274. 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,
  275. 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,
  276. 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,
  277. 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,
  278. 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,
  279. 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,
  280. 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,
  281. 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,
  282. 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,
  283. 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,
  284. 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,
  285. 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,
  286. 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,
  287. 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,
  288. 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,
  289. 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,
  290. 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,
  291. 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,
  292. 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,
  293. 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,
  294. 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,
  295. 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,
  296. 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,
  297. 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
  298. 0, 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,
  299. 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,
  300. 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,
  301. 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,
  302. 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,
  303. //error:0.000022
  304. };
  305. static const uint8_t obmc16[256]={
  306. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  307. 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
  308. 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
  309. 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
  310. 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
  311. 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
  312. 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
  313. 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
  314. 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
  315. 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
  316. 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
  317. 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
  318. 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
  319. 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
  320. 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
  321. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  322. //error:0.000022
  323. };
  324. #endif
  325. //linear *64
  326. static const uint8_t obmc8[64]={
  327. 1, 3, 5, 7, 7, 5, 3, 1,
  328. 3, 9,15,21,21,15, 9, 3,
  329. 5,15,25,35,35,25,15, 5,
  330. 7,21,35,49,49,35,21, 7,
  331. 7,21,35,49,49,35,21, 7,
  332. 5,15,25,35,35,25,15, 5,
  333. 3, 9,15,21,21,15, 9, 3,
  334. 1, 3, 5, 7, 7, 5, 3, 1,
  335. //error:0.000000
  336. };
  337. //linear *64
  338. static const uint8_t obmc4[16]={
  339. 4,12,12, 4,
  340. 12,36,36,12,
  341. 12,36,36,12,
  342. 4,12,12, 4,
  343. //error:0.000000
  344. };
  345. static const uint8_t *obmc_tab[4]={
  346. obmc32, obmc16, obmc8, obmc4
  347. };
  348. typedef struct BlockNode{
  349. int16_t mx;
  350. int16_t my;
  351. uint8_t color[3];
  352. uint8_t type;
  353. //#define TYPE_SPLIT 1
  354. #define BLOCK_INTRA 1
  355. //#define TYPE_NOCOLOR 4
  356. uint8_t level; //FIXME merge into type?
  357. }BlockNode;
  358. #define LOG2_MB_SIZE 4
  359. #define MB_SIZE (1<<LOG2_MB_SIZE)
  360. typedef struct SubBand{
  361. int level;
  362. int stride;
  363. int width;
  364. int height;
  365. int qlog; ///< log(qscale)/log[2^(1/6)]
  366. DWTELEM *buf;
  367. int16_t *x;
  368. DWTELEM *coeff;
  369. struct SubBand *parent;
  370. uint8_t state[/*7*2*/ 7 + 512][32];
  371. }SubBand;
  372. typedef struct Plane{
  373. int width;
  374. int height;
  375. SubBand band[MAX_DECOMPOSITIONS][4];
  376. }Plane;
  377. typedef struct SnowContext{
  378. // 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)
  379. AVCodecContext *avctx;
  380. CABACContext c;
  381. DSPContext dsp;
  382. AVFrame input_picture;
  383. AVFrame current_picture;
  384. AVFrame last_picture;
  385. AVFrame mconly_picture;
  386. // uint8_t q_context[16];
  387. uint8_t header_state[32];
  388. uint8_t block_state[128 + 32*128];
  389. int keyframe;
  390. int always_reset;
  391. int version;
  392. int spatial_decomposition_type;
  393. int temporal_decomposition_type;
  394. int spatial_decomposition_count;
  395. int temporal_decomposition_count;
  396. DWTELEM *spatial_dwt_buffer;
  397. int colorspace_type;
  398. int chroma_h_shift;
  399. int chroma_v_shift;
  400. int spatial_scalability;
  401. int qlog;
  402. int lambda;
  403. int lambda2;
  404. int mv_scale;
  405. int qbias;
  406. #define QBIAS_SHIFT 3
  407. int b_width;
  408. int b_height;
  409. int block_max_depth;
  410. Plane plane[MAX_PLANES];
  411. BlockNode *block;
  412. 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)
  413. }SnowContext;
  414. #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
  415. static const uint8_t qexp[8]={
  416. 128, 140, 152, 166, 181, 197, 215, 235
  417. // 64, 70, 76, 83, 91, 99, 108, 117
  418. // 32, 35, 38, 41, 45, 49, 54, 59
  419. // 16, 17, 19, 21, 23, 25, 27, 29
  420. // 8, 9, 10, 10, 11, 12, 13, 15
  421. };
  422. static inline int mirror(int v, int m){
  423. if (v<0) return -v;
  424. else if(v>m) return 2*m-v;
  425. else return v;
  426. }
  427. static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){
  428. int i;
  429. if(v){
  430. const int a= ABS(v);
  431. const int e= av_log2(a);
  432. #if 1
  433. const int el= FFMIN(e, 10);
  434. put_cabac(c, state+0, 0);
  435. for(i=0; i<el; i++){
  436. put_cabac(c, state+1+i, 1); //1..10
  437. }
  438. for(; i<e; i++){
  439. put_cabac(c, state+1+9, 1); //1..10
  440. }
  441. put_cabac(c, state+1+FFMIN(i,9), 0);
  442. for(i=e-1; i>=el; i--){
  443. put_cabac(c, state+22+9, (a>>i)&1); //22..31
  444. }
  445. for(; i>=0; i--){
  446. put_cabac(c, state+22+i, (a>>i)&1); //22..31
  447. }
  448. if(is_signed)
  449. put_cabac(c, state+11 + el, v < 0); //11..21
  450. #else
  451. put_cabac(c, state+0, 0);
  452. if(e<=9){
  453. for(i=0; i<e; i++){
  454. put_cabac(c, state+1+i, 1); //1..10
  455. }
  456. put_cabac(c, state+1+i, 0);
  457. for(i=e-1; i>=0; i--){
  458. put_cabac(c, state+22+i, (a>>i)&1); //22..31
  459. }
  460. if(is_signed)
  461. put_cabac(c, state+11 + e, v < 0); //11..21
  462. }else{
  463. for(i=0; i<e; i++){
  464. put_cabac(c, state+1+FFMIN(i,9), 1); //1..10
  465. }
  466. put_cabac(c, state+1+FFMIN(i,9), 0);
  467. for(i=e-1; i>=0; i--){
  468. put_cabac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
  469. }
  470. if(is_signed)
  471. put_cabac(c, state+11 + FFMIN(e,10), v < 0); //11..21
  472. }
  473. #endif
  474. }else{
  475. put_cabac(c, state+0, 1);
  476. }
  477. }
  478. static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed){
  479. if(get_cabac(c, state+0))
  480. return 0;
  481. else{
  482. int i, e, a;
  483. e= 0;
  484. while(get_cabac(c, state+1 + FFMIN(e,9))){ //1..10
  485. e++;
  486. }
  487. a= 1;
  488. for(i=e-1; i>=0; i--){
  489. a += a + get_cabac(c, state+22 + FFMIN(i,9)); //22..31
  490. }
  491. if(is_signed && get_cabac(c, state+11 + FFMIN(e,10))) //11..21
  492. return -a;
  493. else
  494. return a;
  495. }
  496. }
  497. static inline void put_symbol2(CABACContext *c, uint8_t *state, int v, int log2){
  498. int i;
  499. int r= log2>=0 ? 1<<log2 : 1;
  500. assert(v>=0);
  501. assert(log2>=-4);
  502. while(v >= r){
  503. put_cabac(c, state+4+log2, 1);
  504. v -= r;
  505. log2++;
  506. if(log2>0) r+=r;
  507. }
  508. put_cabac(c, state+4+log2, 0);
  509. for(i=log2-1; i>=0; i--){
  510. put_cabac(c, state+31-i, (v>>i)&1);
  511. }
  512. }
  513. static inline int get_symbol2(CABACContext *c, uint8_t *state, int log2){
  514. int i;
  515. int r= log2>=0 ? 1<<log2 : 1;
  516. int v=0;
  517. assert(log2>=-4);
  518. while(get_cabac(c, state+4+log2)){
  519. v+= r;
  520. log2++;
  521. if(log2>0) r+=r;
  522. }
  523. for(i=log2-1; i>=0; i--){
  524. v+= get_cabac(c, state+31-i)<<i;
  525. }
  526. return v;
  527. }
  528. 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){
  529. const int mirror_left= !highpass;
  530. const int mirror_right= (width&1) ^ highpass;
  531. const int w= (width>>1) - 1 + (highpass & width);
  532. int i;
  533. #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
  534. if(mirror_left){
  535. dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
  536. dst += dst_step;
  537. src += src_step;
  538. }
  539. for(i=0; i<w; i++){
  540. dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
  541. }
  542. if(mirror_right){
  543. dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
  544. }
  545. }
  546. 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){
  547. const int mirror_left= !highpass;
  548. const int mirror_right= (width&1) ^ highpass;
  549. const int w= (width>>1) - 1 + (highpass & width);
  550. int i;
  551. if(mirror_left){
  552. int r= 3*2*ref[0];
  553. r += r>>4;
  554. r += r>>8;
  555. dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
  556. dst += dst_step;
  557. src += src_step;
  558. }
  559. for(i=0; i<w; i++){
  560. int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
  561. r += r>>4;
  562. r += r>>8;
  563. dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
  564. }
  565. if(mirror_right){
  566. int r= 3*2*ref[w*ref_step];
  567. r += r>>4;
  568. r += r>>8;
  569. dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
  570. }
  571. }
  572. static void inplace_lift(DWTELEM *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
  573. int x, i;
  574. for(x=start; x<width; x+=2){
  575. int64_t sum=0;
  576. for(i=0; i<n; i++){
  577. int x2= x + 2*i - n + 1;
  578. if (x2< 0) x2= -x2;
  579. else if(x2>=width) x2= 2*width-x2-2;
  580. sum += coeffs[i]*(int64_t)dst[x2];
  581. }
  582. if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
  583. else dst[x] += (sum + (1<<shift)/2)>>shift;
  584. }
  585. }
  586. static void inplace_liftV(DWTELEM *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
  587. int x, y, i;
  588. for(y=start; y<height; y+=2){
  589. for(x=0; x<width; x++){
  590. int64_t sum=0;
  591. for(i=0; i<n; i++){
  592. int y2= y + 2*i - n + 1;
  593. if (y2< 0) y2= -y2;
  594. else if(y2>=height) y2= 2*height-y2-2;
  595. sum += coeffs[i]*(int64_t)dst[x + y2*stride];
  596. }
  597. if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
  598. else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
  599. }
  600. }
  601. }
  602. #define SCALEX 1
  603. #define LX0 0
  604. #define LX1 1
  605. #if 0 // more accurate 9/7
  606. #define N1 2
  607. #define SHIFT1 14
  608. #define COEFFS1 (int[]){-25987,-25987}
  609. #define N2 2
  610. #define SHIFT2 19
  611. #define COEFFS2 (int[]){-27777,-27777}
  612. #define N3 2
  613. #define SHIFT3 15
  614. #define COEFFS3 (int[]){28931,28931}
  615. #define N4 2
  616. #define SHIFT4 15
  617. #define COEFFS4 (int[]){14533,14533}
  618. #elif 1 // 13/7 CRF
  619. #define N1 4
  620. #define SHIFT1 4
  621. #define COEFFS1 (int[]){1,-9,-9,1}
  622. #define N2 4
  623. #define SHIFT2 4
  624. #define COEFFS2 (int[]){-1,5,5,-1}
  625. #define N3 0
  626. #define SHIFT3 1
  627. #define COEFFS3 NULL
  628. #define N4 0
  629. #define SHIFT4 1
  630. #define COEFFS4 NULL
  631. #elif 1 // 3/5
  632. #define LX0 1
  633. #define LX1 0
  634. #define SCALEX 0.5
  635. #define N1 2
  636. #define SHIFT1 1
  637. #define COEFFS1 (int[]){1,1}
  638. #define N2 2
  639. #define SHIFT2 2
  640. #define COEFFS2 (int[]){-1,-1}
  641. #define N3 0
  642. #define SHIFT3 0
  643. #define COEFFS3 NULL
  644. #define N4 0
  645. #define SHIFT4 0
  646. #define COEFFS4 NULL
  647. #elif 1 // 11/5
  648. #define N1 0
  649. #define SHIFT1 1
  650. #define COEFFS1 NULL
  651. #define N2 2
  652. #define SHIFT2 2
  653. #define COEFFS2 (int[]){-1,-1}
  654. #define N3 2
  655. #define SHIFT3 0
  656. #define COEFFS3 (int[]){-1,-1}
  657. #define N4 4
  658. #define SHIFT4 7
  659. #define COEFFS4 (int[]){-5,29,29,-5}
  660. #define SCALEX 4
  661. #elif 1 // 9/7 CDF
  662. #define N1 2
  663. #define SHIFT1 7
  664. #define COEFFS1 (int[]){-203,-203}
  665. #define N2 2
  666. #define SHIFT2 12
  667. #define COEFFS2 (int[]){-217,-217}
  668. #define N3 2
  669. #define SHIFT3 7
  670. #define COEFFS3 (int[]){113,113}
  671. #define N4 2
  672. #define SHIFT4 9
  673. #define COEFFS4 (int[]){227,227}
  674. #define SCALEX 1
  675. #elif 1 // 7/5 CDF
  676. #define N1 0
  677. #define SHIFT1 1
  678. #define COEFFS1 NULL
  679. #define N2 2
  680. #define SHIFT2 2
  681. #define COEFFS2 (int[]){-1,-1}
  682. #define N3 2
  683. #define SHIFT3 0
  684. #define COEFFS3 (int[]){-1,-1}
  685. #define N4 2
  686. #define SHIFT4 4
  687. #define COEFFS4 (int[]){3,3}
  688. #elif 1 // 9/7 MN
  689. #define N1 4
  690. #define SHIFT1 4
  691. #define COEFFS1 (int[]){1,-9,-9,1}
  692. #define N2 2
  693. #define SHIFT2 2
  694. #define COEFFS2 (int[]){1,1}
  695. #define N3 0
  696. #define SHIFT3 1
  697. #define COEFFS3 NULL
  698. #define N4 0
  699. #define SHIFT4 1
  700. #define COEFFS4 NULL
  701. #else // 13/7 CRF
  702. #define N1 4
  703. #define SHIFT1 4
  704. #define COEFFS1 (int[]){1,-9,-9,1}
  705. #define N2 4
  706. #define SHIFT2 4
  707. #define COEFFS2 (int[]){-1,5,5,-1}
  708. #define N3 0
  709. #define SHIFT3 1
  710. #define COEFFS3 NULL
  711. #define N4 0
  712. #define SHIFT4 1
  713. #define COEFFS4 NULL
  714. #endif
  715. static void horizontal_decomposeX(DWTELEM *b, int width){
  716. DWTELEM temp[width];
  717. const int width2= width>>1;
  718. const int w2= (width+1)>>1;
  719. int A1,A2,A3,A4, x;
  720. inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
  721. inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
  722. inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
  723. inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
  724. for(x=0; x<width2; x++){
  725. temp[x ]= b[2*x ];
  726. temp[x+w2]= b[2*x + 1];
  727. }
  728. if(width&1)
  729. temp[x ]= b[2*x ];
  730. memcpy(b, temp, width*sizeof(int));
  731. }
  732. static void horizontal_composeX(DWTELEM *b, int width){
  733. DWTELEM temp[width];
  734. const int width2= width>>1;
  735. int A1,A2,A3,A4, x;
  736. const int w2= (width+1)>>1;
  737. memcpy(temp, b, width*sizeof(int));
  738. for(x=0; x<width2; x++){
  739. b[2*x ]= temp[x ];
  740. b[2*x + 1]= temp[x+w2];
  741. }
  742. if(width&1)
  743. b[2*x ]= temp[x ];
  744. inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
  745. inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
  746. inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
  747. inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
  748. }
  749. static void spatial_decomposeX(DWTELEM *buffer, int width, int height, int stride){
  750. int x, y;
  751. for(y=0; y<height; y++){
  752. for(x=0; x<width; x++){
  753. buffer[y*stride + x] *= SCALEX;
  754. }
  755. }
  756. for(y=0; y<height; y++){
  757. horizontal_decomposeX(buffer + y*stride, width);
  758. }
  759. inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
  760. inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
  761. inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
  762. inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
  763. }
  764. static void spatial_composeX(DWTELEM *buffer, int width, int height, int stride){
  765. int x, y;
  766. inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
  767. inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
  768. inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
  769. inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
  770. for(y=0; y<height; y++){
  771. horizontal_composeX(buffer + y*stride, width);
  772. }
  773. for(y=0; y<height; y++){
  774. for(x=0; x<width; x++){
  775. buffer[y*stride + x] /= SCALEX;
  776. }
  777. }
  778. }
  779. static void horizontal_decompose53i(DWTELEM *b, int width){
  780. DWTELEM temp[width];
  781. const int width2= width>>1;
  782. int A1,A2,A3,A4, x;
  783. const int w2= (width+1)>>1;
  784. for(x=0; x<width2; x++){
  785. temp[x ]= b[2*x ];
  786. temp[x+w2]= b[2*x + 1];
  787. }
  788. if(width&1)
  789. temp[x ]= b[2*x ];
  790. #if 0
  791. A2= temp[1 ];
  792. A4= temp[0 ];
  793. A1= temp[0+width2];
  794. A1 -= (A2 + A4)>>1;
  795. A4 += (A1 + 1)>>1;
  796. b[0+width2] = A1;
  797. b[0 ] = A4;
  798. for(x=1; x+1<width2; x+=2){
  799. A3= temp[x+width2];
  800. A4= temp[x+1 ];
  801. A3 -= (A2 + A4)>>1;
  802. A2 += (A1 + A3 + 2)>>2;
  803. b[x+width2] = A3;
  804. b[x ] = A2;
  805. A1= temp[x+1+width2];
  806. A2= temp[x+2 ];
  807. A1 -= (A2 + A4)>>1;
  808. A4 += (A1 + A3 + 2)>>2;
  809. b[x+1+width2] = A1;
  810. b[x+1 ] = A4;
  811. }
  812. A3= temp[width-1];
  813. A3 -= A2;
  814. A2 += (A1 + A3 + 2)>>2;
  815. b[width -1] = A3;
  816. b[width2-1] = A2;
  817. #else
  818. lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
  819. lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
  820. #endif
  821. }
  822. static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  823. int i;
  824. for(i=0; i<width; i++){
  825. b1[i] -= (b0[i] + b2[i])>>1;
  826. }
  827. }
  828. static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  829. int i;
  830. for(i=0; i<width; i++){
  831. b1[i] += (b0[i] + b2[i] + 2)>>2;
  832. }
  833. }
  834. static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
  835. int y;
  836. DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
  837. DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
  838. for(y=-2; y<height; y+=2){
  839. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  840. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  841. {START_TIMER
  842. if(b1 <= b3) horizontal_decompose53i(b2, width);
  843. if(y+2 < height) horizontal_decompose53i(b3, width);
  844. STOP_TIMER("horizontal_decompose53i")}
  845. {START_TIMER
  846. if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
  847. if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
  848. STOP_TIMER("vertical_decompose53i*")}
  849. b0=b2;
  850. b1=b3;
  851. }
  852. }
  853. #define lift5 lift
  854. #if 1
  855. #define W_AM 3
  856. #define W_AO 0
  857. #define W_AS 1
  858. #define W_BM 1
  859. #define W_BO 8
  860. #define W_BS 4
  861. #undef lift5
  862. #define W_CM 9999
  863. #define W_CO 2
  864. #define W_CS 2
  865. #define W_DM 15
  866. #define W_DO 16
  867. #define W_DS 5
  868. #elif 0
  869. #define W_AM 55
  870. #define W_AO 16
  871. #define W_AS 5
  872. #define W_BM 3
  873. #define W_BO 32
  874. #define W_BS 6
  875. #define W_CM 127
  876. #define W_CO 64
  877. #define W_CS 7
  878. #define W_DM 7
  879. #define W_DO 8
  880. #define W_DS 4
  881. #elif 0
  882. #define W_AM 97
  883. #define W_AO 32
  884. #define W_AS 6
  885. #define W_BM 63
  886. #define W_BO 512
  887. #define W_BS 10
  888. #define W_CM 13
  889. #define W_CO 8
  890. #define W_CS 4
  891. #define W_DM 15
  892. #define W_DO 16
  893. #define W_DS 5
  894. #else
  895. #define W_AM 203
  896. #define W_AO 64
  897. #define W_AS 7
  898. #define W_BM 217
  899. #define W_BO 2048
  900. #define W_BS 12
  901. #define W_CM 113
  902. #define W_CO 64
  903. #define W_CS 7
  904. #define W_DM 227
  905. #define W_DO 128
  906. #define W_DS 9
  907. #endif
  908. static void horizontal_decompose97i(DWTELEM *b, int width){
  909. DWTELEM temp[width];
  910. const int w2= (width+1)>>1;
  911. lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
  912. lift (temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
  913. lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
  914. lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
  915. }
  916. static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  917. int i;
  918. for(i=0; i<width; i++){
  919. b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
  920. }
  921. }
  922. static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  923. int i;
  924. for(i=0; i<width; i++){
  925. #ifdef lift5
  926. b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
  927. #else
  928. int r= 3*(b0[i] + b2[i]);
  929. r+= r>>4;
  930. r+= r>>8;
  931. b1[i] += (r+W_CO)>>W_CS;
  932. #endif
  933. }
  934. }
  935. static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  936. int i;
  937. for(i=0; i<width; i++){
  938. b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  939. }
  940. }
  941. static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  942. int i;
  943. for(i=0; i<width; i++){
  944. b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  945. }
  946. }
  947. static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
  948. int y;
  949. DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
  950. DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
  951. DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
  952. DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
  953. for(y=-4; y<height; y+=2){
  954. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  955. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  956. {START_TIMER
  957. if(b3 <= b5) horizontal_decompose97i(b4, width);
  958. if(y+4 < height) horizontal_decompose97i(b5, width);
  959. if(width>400){
  960. STOP_TIMER("horizontal_decompose97i")
  961. }}
  962. {START_TIMER
  963. if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
  964. if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
  965. if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
  966. if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
  967. if(width>400){
  968. STOP_TIMER("vertical_decompose97i")
  969. }}
  970. b0=b2;
  971. b1=b3;
  972. b2=b4;
  973. b3=b5;
  974. }
  975. }
  976. void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  977. int level;
  978. for(level=0; level<decomposition_count; level++){
  979. switch(type){
  980. case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
  981. case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
  982. case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
  983. }
  984. }
  985. }
  986. static void horizontal_compose53i(DWTELEM *b, int width){
  987. DWTELEM temp[width];
  988. const int width2= width>>1;
  989. const int w2= (width+1)>>1;
  990. int A1,A2,A3,A4, x;
  991. #if 0
  992. A2= temp[1 ];
  993. A4= temp[0 ];
  994. A1= temp[0+width2];
  995. A1 -= (A2 + A4)>>1;
  996. A4 += (A1 + 1)>>1;
  997. b[0+width2] = A1;
  998. b[0 ] = A4;
  999. for(x=1; x+1<width2; x+=2){
  1000. A3= temp[x+width2];
  1001. A4= temp[x+1 ];
  1002. A3 -= (A2 + A4)>>1;
  1003. A2 += (A1 + A3 + 2)>>2;
  1004. b[x+width2] = A3;
  1005. b[x ] = A2;
  1006. A1= temp[x+1+width2];
  1007. A2= temp[x+2 ];
  1008. A1 -= (A2 + A4)>>1;
  1009. A4 += (A1 + A3 + 2)>>2;
  1010. b[x+1+width2] = A1;
  1011. b[x+1 ] = A4;
  1012. }
  1013. A3= temp[width-1];
  1014. A3 -= A2;
  1015. A2 += (A1 + A3 + 2)>>2;
  1016. b[width -1] = A3;
  1017. b[width2-1] = A2;
  1018. #else
  1019. lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
  1020. lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
  1021. #endif
  1022. for(x=0; x<width2; x++){
  1023. b[2*x ]= temp[x ];
  1024. b[2*x + 1]= temp[x+w2];
  1025. }
  1026. if(width&1)
  1027. b[2*x ]= temp[x ];
  1028. }
  1029. static void vertical_compose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  1030. int i;
  1031. for(i=0; i<width; i++){
  1032. b1[i] += (b0[i] + b2[i])>>1;
  1033. }
  1034. }
  1035. static void vertical_compose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  1036. int i;
  1037. for(i=0; i<width; i++){
  1038. b1[i] -= (b0[i] + b2[i] + 2)>>2;
  1039. }
  1040. }
  1041. static void spatial_compose53i(DWTELEM *buffer, int width, int height, int stride){
  1042. int y;
  1043. DWTELEM *b0= buffer + mirror(-1-1, height-1)*stride;
  1044. DWTELEM *b1= buffer + mirror(-1 , height-1)*stride;
  1045. for(y=-1; y<=height; y+=2){
  1046. DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
  1047. DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
  1048. {START_TIMER
  1049. if(b1 <= b3) vertical_compose53iL0(b1, b2, b3, width);
  1050. if(b0 <= b2) vertical_compose53iH0(b0, b1, b2, width);
  1051. STOP_TIMER("vertical_compose53i*")}
  1052. {START_TIMER
  1053. if(y-1 >= 0) horizontal_compose53i(b0, width);
  1054. if(b0 <= b2) horizontal_compose53i(b1, width);
  1055. STOP_TIMER("horizontal_compose53i")}
  1056. b0=b2;
  1057. b1=b3;
  1058. }
  1059. }
  1060. static void horizontal_compose97i(DWTELEM *b, int width){
  1061. DWTELEM temp[width];
  1062. const int w2= (width+1)>>1;
  1063. lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
  1064. lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
  1065. lift (b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
  1066. lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
  1067. }
  1068. static void vertical_compose97iH0(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_compose97iH1(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_compose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  1088. int i;
  1089. for(i=0; i<width; i++){
  1090. b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
  1091. }
  1092. }
  1093. static void vertical_compose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
  1094. int i;
  1095. for(i=0; i<width; i++){
  1096. b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
  1097. }
  1098. }
  1099. static void spatial_compose97i(DWTELEM *buffer, int width, int height, int stride){
  1100. int y;
  1101. DWTELEM *b0= buffer + mirror(-3-1, height-1)*stride;
  1102. DWTELEM *b1= buffer + mirror(-3 , height-1)*stride;
  1103. DWTELEM *b2= buffer + mirror(-3+1, height-1)*stride;
  1104. DWTELEM *b3= buffer + mirror(-3+2, height-1)*stride;
  1105. for(y=-3; y<=height; y+=2){
  1106. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  1107. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  1108. if(stride == width && y+4 < height && 0){
  1109. int x;
  1110. for(x=0; x<width/2; x++)
  1111. b5[x] += 64*2;
  1112. for(; x<width; x++)
  1113. b5[x] += 169*2;
  1114. }
  1115. {START_TIMER
  1116. if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
  1117. if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
  1118. if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
  1119. if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
  1120. if(width>400){
  1121. STOP_TIMER("vertical_compose97i")}}
  1122. {START_TIMER
  1123. if(y-1>= 0) horizontal_compose97i(b0, width);
  1124. if(b0 <= b2) horizontal_compose97i(b1, width);
  1125. if(width>400 && b0 <= b2){
  1126. STOP_TIMER("horizontal_compose97i")}}
  1127. b0=b2;
  1128. b1=b3;
  1129. b2=b4;
  1130. b3=b5;
  1131. }
  1132. }
  1133. void ff_spatial_idwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  1134. int level;
  1135. for(level=decomposition_count-1; level>=0; level--){
  1136. switch(type){
  1137. case 0: spatial_compose97i(buffer, width>>level, height>>level, stride<<level); break;
  1138. case 1: spatial_compose53i(buffer, width>>level, height>>level, stride<<level); break;
  1139. case 2: spatial_composeX (buffer, width>>level, height>>level, stride<<level); break;
  1140. }
  1141. }
  1142. }
  1143. static void encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1144. const int w= b->width;
  1145. const int h= b->height;
  1146. int x, y;
  1147. if(1){
  1148. int run=0;
  1149. int runs[w*h];
  1150. int run_index=0;
  1151. for(y=0; y<h; y++){
  1152. for(x=0; x<w; x++){
  1153. int v, p=0;
  1154. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1155. v= src[x + y*stride];
  1156. if(y){
  1157. t= src[x + (y-1)*stride];
  1158. if(x){
  1159. lt= src[x - 1 + (y-1)*stride];
  1160. }
  1161. if(x + 1 < w){
  1162. rt= src[x + 1 + (y-1)*stride];
  1163. }
  1164. }
  1165. if(x){
  1166. l= src[x - 1 + y*stride];
  1167. /*if(x > 1){
  1168. if(orientation==1) ll= src[y + (x-2)*stride];
  1169. else ll= src[x - 2 + y*stride];
  1170. }*/
  1171. }
  1172. if(parent){
  1173. int px= x>>1;
  1174. int py= y>>1;
  1175. if(px<b->parent->width && py<b->parent->height)
  1176. p= parent[px + py*2*stride];
  1177. }
  1178. if(!(/*ll|*/l|lt|t|rt|p)){
  1179. if(v){
  1180. runs[run_index++]= run;
  1181. run=0;
  1182. }else{
  1183. run++;
  1184. }
  1185. }
  1186. }
  1187. }
  1188. runs[run_index++]= run;
  1189. run_index=0;
  1190. run= runs[run_index++];
  1191. put_symbol2(&s->c, b->state[1], run, 3);
  1192. for(y=0; y<h; y++){
  1193. for(x=0; x<w; x++){
  1194. int v, p=0;
  1195. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1196. v= src[x + y*stride];
  1197. if(y){
  1198. t= src[x + (y-1)*stride];
  1199. if(x){
  1200. lt= src[x - 1 + (y-1)*stride];
  1201. }
  1202. if(x + 1 < w){
  1203. rt= src[x + 1 + (y-1)*stride];
  1204. }
  1205. }
  1206. if(x){
  1207. l= src[x - 1 + y*stride];
  1208. /*if(x > 1){
  1209. if(orientation==1) ll= src[y + (x-2)*stride];
  1210. else ll= src[x - 2 + y*stride];
  1211. }*/
  1212. }
  1213. if(parent){
  1214. int px= x>>1;
  1215. int py= y>>1;
  1216. if(px<b->parent->width && py<b->parent->height)
  1217. p= parent[px + py*2*stride];
  1218. }
  1219. if(/*ll|*/l|lt|t|rt|p){
  1220. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1221. put_cabac(&s->c, &b->state[0][context], !!v);
  1222. }else{
  1223. if(!run){
  1224. run= runs[run_index++];
  1225. put_symbol2(&s->c, b->state[1], run, 3);
  1226. assert(v);
  1227. }else{
  1228. run--;
  1229. assert(!v);
  1230. }
  1231. }
  1232. if(v){
  1233. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1234. put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
  1235. put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
  1236. }
  1237. }
  1238. }
  1239. }
  1240. }
  1241. static void encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1242. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  1243. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  1244. encode_subband_c0run(s, b, src, parent, stride, orientation);
  1245. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  1246. }
  1247. static inline void decode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1248. const int w= b->width;
  1249. const int h= b->height;
  1250. int x,y;
  1251. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  1252. int qmul= qexp[qlog&7]<<(qlog>>3);
  1253. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1254. START_TIMER
  1255. if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){
  1256. qadd= 0;
  1257. qmul= 1<<QEXPSHIFT;
  1258. }
  1259. if(1){
  1260. int run;
  1261. int index=0;
  1262. int prev_index=-1;
  1263. int prev2_index=0;
  1264. int parent_index= 0;
  1265. int prev_parent_index= 0;
  1266. for(y=0; y<b->height; y++)
  1267. memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
  1268. run= get_symbol2(&s->c, b->state[1], 3);
  1269. for(y=0; y<h; y++){
  1270. int v=0;
  1271. int lt=0, t=0, rt=0;
  1272. if(y && b->x[prev_index] == 0){
  1273. rt= b->coeff[prev_index];
  1274. }
  1275. for(x=0; x<w; x++){
  1276. int p=0;
  1277. const int l= v;
  1278. lt= t; t= rt;
  1279. if(y){
  1280. if(b->x[prev_index] <= x)
  1281. prev_index++;
  1282. if(b->x[prev_index] == x + 1)
  1283. rt= b->coeff[prev_index];
  1284. else
  1285. rt=0;
  1286. }
  1287. if(parent){
  1288. if(x>>1 > b->parent->x[parent_index]){
  1289. parent_index++;
  1290. }
  1291. if(x>>1 == b->parent->x[parent_index]){
  1292. p= b->parent->coeff[parent_index];
  1293. }
  1294. }
  1295. if(/*ll|*/l|lt|t|rt|p){
  1296. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1297. v=get_cabac(&s->c, &b->state[0][context]);
  1298. }else{
  1299. if(!run){
  1300. run= get_symbol2(&s->c, b->state[1], 3);
  1301. v=1;
  1302. }else{
  1303. run--;
  1304. v=0;
  1305. if(y && parent){
  1306. int max_run;
  1307. max_run= FFMIN(run, b->x[prev_index] - x - 2);
  1308. max_run= FFMIN(max_run, 2*b->parent->x[parent_index] - x - 1);
  1309. x+= max_run;
  1310. run-= max_run;
  1311. }
  1312. }
  1313. }
  1314. if(v){
  1315. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1316. v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1;
  1317. if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]])){
  1318. src[x + y*stride]=-(( v*qmul + qadd)>>(QEXPSHIFT));
  1319. v= -v;
  1320. }else{
  1321. src[x + y*stride]= (( v*qmul + qadd)>>(QEXPSHIFT));
  1322. }
  1323. b->x[index]=x; //FIXME interleave x/coeff
  1324. b->coeff[index++]= v;
  1325. }
  1326. }
  1327. b->x[index++]= w+1; //end marker
  1328. prev_index= prev2_index;
  1329. prev2_index= index;
  1330. if(parent){
  1331. while(b->parent->x[parent_index] != b->parent->width+1)
  1332. parent_index++;
  1333. parent_index++;
  1334. if(y&1){
  1335. prev_parent_index= parent_index;
  1336. }else{
  1337. parent_index= prev_parent_index;
  1338. }
  1339. }
  1340. }
  1341. b->x[index++]= w+1; //end marker
  1342. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  1343. STOP_TIMER("decode_subband")
  1344. }
  1345. return;
  1346. }
  1347. }
  1348. static void reset_contexts(SnowContext *s){
  1349. int plane_index, level, orientation;
  1350. for(plane_index=0; plane_index<3; plane_index++){
  1351. for(level=0; level<s->spatial_decomposition_count; level++){
  1352. for(orientation=level ? 1:0; orientation<4; orientation++){
  1353. memset(s->plane[plane_index].band[level][orientation].state, 0, sizeof(s->plane[plane_index].band[level][orientation].state));
  1354. }
  1355. }
  1356. }
  1357. memset(s->header_state, 0, sizeof(s->header_state));
  1358. memset(s->block_state, 0, sizeof(s->block_state));
  1359. }
  1360. static int alloc_blocks(SnowContext *s){
  1361. int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
  1362. int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
  1363. s->b_width = w;
  1364. s->b_height= h;
  1365. s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
  1366. return 0;
  1367. }
  1368. static inline void copy_cabac_state(CABACContext *d, CABACContext *s){
  1369. PutBitContext bak= d->pb;
  1370. *d= *s;
  1371. d->pb= bak;
  1372. }
  1373. //near copy & paste from dsputil, FIXME
  1374. static int pix_sum(uint8_t * pix, int line_size, int w)
  1375. {
  1376. int s, i, j;
  1377. s = 0;
  1378. for (i = 0; i < w; i++) {
  1379. for (j = 0; j < w; j++) {
  1380. s += pix[0];
  1381. pix ++;
  1382. }
  1383. pix += line_size - w;
  1384. }
  1385. return s;
  1386. }
  1387. //near copy & paste from dsputil, FIXME
  1388. static int pix_norm1(uint8_t * pix, int line_size, int w)
  1389. {
  1390. int s, i, j;
  1391. uint32_t *sq = squareTbl + 256;
  1392. s = 0;
  1393. for (i = 0; i < w; i++) {
  1394. for (j = 0; j < w; j ++) {
  1395. s += sq[pix[0]];
  1396. pix ++;
  1397. }
  1398. pix += line_size - w;
  1399. }
  1400. return s;
  1401. }
  1402. 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){
  1403. const int w= s->b_width << s->block_max_depth;
  1404. const int rem_depth= s->block_max_depth - level;
  1405. const int index= (x + y*w) << rem_depth;
  1406. const int block_w= 1<<rem_depth;
  1407. BlockNode block;
  1408. int i,j;
  1409. block.color[0]= l;
  1410. block.color[1]= cb;
  1411. block.color[2]= cr;
  1412. block.mx= mx;
  1413. block.my= my;
  1414. block.type= type;
  1415. block.level= level;
  1416. for(j=0; j<block_w; j++){
  1417. for(i=0; i<block_w; i++){
  1418. s->block[index + i + j*w]= block;
  1419. }
  1420. }
  1421. }
  1422. 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){
  1423. const int offset[3]= {
  1424. y*c-> stride + x,
  1425. ((y*c->uvstride + x)>>1),
  1426. ((y*c->uvstride + x)>>1),
  1427. };
  1428. int i;
  1429. for(i=0; i<3; i++){
  1430. c->src[0][i]= src [i];
  1431. c->ref[0][i]= ref [i] + offset[i];
  1432. }
  1433. assert(!ref_index);
  1434. }
  1435. //FIXME copy&paste
  1436. #define P_LEFT P[1]
  1437. #define P_TOP P[2]
  1438. #define P_TOPRIGHT P[3]
  1439. #define P_MEDIAN P[4]
  1440. #define P_MV1 P[9]
  1441. #define FLAG_QPEL 1 //must be 1
  1442. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  1443. uint8_t p_buffer[1024];
  1444. uint8_t i_buffer[1024];
  1445. uint8_t p_state[sizeof(s->block_state)];
  1446. uint8_t i_state[sizeof(s->block_state)];
  1447. CABACContext pc, ic;
  1448. PutBitContext pbbak= s->c.pb;
  1449. int score, score2, iscore, i_len, p_len, block_s, sum;
  1450. const int w= s->b_width << s->block_max_depth;
  1451. const int h= s->b_height << s->block_max_depth;
  1452. const int rem_depth= s->block_max_depth - level;
  1453. const int index= (x + y*w) << rem_depth;
  1454. const int block_w= 1<<(LOG2_MB_SIZE - level);
  1455. static BlockNode null_block= { //FIXME add border maybe
  1456. .color= {128,128,128},
  1457. .mx= 0,
  1458. .my= 0,
  1459. .type= 0,
  1460. .level= 0,
  1461. };
  1462. int trx= (x+1)<<rem_depth;
  1463. int try= (y+1)<<rem_depth;
  1464. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1465. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1466. BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  1467. BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  1468. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1469. BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1470. int pl = left->color[0];
  1471. int pcb= left->color[1];
  1472. int pcr= left->color[2];
  1473. int pmx= mid_pred(left->mx, top->mx, tr->mx);
  1474. int pmy= mid_pred(left->my, top->my, tr->my);
  1475. int mx=0, my=0;
  1476. int l,cr,cb, i;
  1477. const int stride= s->current_picture.linesize[0];
  1478. const int uvstride= s->current_picture.linesize[1];
  1479. const int instride= s->input_picture.linesize[0];
  1480. const int uvinstride= s->input_picture.linesize[1];
  1481. uint8_t *new_l = s->input_picture.data[0] + (x + y* instride)*block_w;
  1482. uint8_t *new_cb= s->input_picture.data[1] + (x + y*uvinstride)*block_w/2;
  1483. uint8_t *new_cr= s->input_picture.data[2] + (x + y*uvinstride)*block_w/2;
  1484. uint8_t current_mb[3][stride*block_w];
  1485. uint8_t *current_data[3]= {&current_mb[0][0], &current_mb[1][0], &current_mb[2][0]};
  1486. int P[10][2];
  1487. int16_t last_mv[3][2];
  1488. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  1489. const int shift= 1+qpel;
  1490. MotionEstContext *c= &s->m.me;
  1491. int mx_context= av_log2(2*ABS(left->mx - top->mx));
  1492. int my_context= av_log2(2*ABS(left->my - top->my));
  1493. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1494. assert(sizeof(s->block_state) >= 256);
  1495. if(s->keyframe){
  1496. set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, BLOCK_INTRA);
  1497. return 0;
  1498. }
  1499. //FIXME optimize
  1500. for(i=0; i<block_w; i++)
  1501. memcpy(&current_mb[0][0] + stride*i, new_l + instride*i, block_w);
  1502. for(i=0; i<block_w>>1; i++)
  1503. memcpy(&current_mb[1][0] + uvstride*i, new_cb + uvinstride*i, block_w>>1);
  1504. for(i=0; i<block_w>>1; i++)
  1505. memcpy(&current_mb[2][0] + uvstride*i, new_cr + uvinstride*i, block_w>>1);
  1506. // clip predictors / edge ?
  1507. P_LEFT[0]= left->mx;
  1508. P_LEFT[1]= left->my;
  1509. P_TOP [0]= top->mx;
  1510. P_TOP [1]= top->my;
  1511. P_TOPRIGHT[0]= tr->mx;
  1512. P_TOPRIGHT[1]= tr->my;
  1513. last_mv[0][0]= s->block[index].mx;
  1514. last_mv[0][1]= s->block[index].my;
  1515. last_mv[1][0]= right->mx;
  1516. last_mv[1][1]= right->my;
  1517. last_mv[2][0]= bottom->mx;
  1518. last_mv[2][1]= bottom->my;
  1519. s->m.mb_stride=2;
  1520. s->m.mb_x=
  1521. s->m.mb_y= 0;
  1522. s->m.me.skip= 0;
  1523. init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0);
  1524. assert(s->m.me. stride == stride);
  1525. assert(s->m.me.uvstride == uvstride);
  1526. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1527. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1528. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1529. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  1530. c->xmin = - x*block_w - 16+2;
  1531. c->ymin = - y*block_w - 16+2;
  1532. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
  1533. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
  1534. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1535. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  1536. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  1537. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1538. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1539. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  1540. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1541. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1542. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1543. if (!y) {
  1544. c->pred_x= P_LEFT[0];
  1545. c->pred_y= P_LEFT[1];
  1546. } else {
  1547. c->pred_x = P_MEDIAN[0];
  1548. c->pred_y = P_MEDIAN[1];
  1549. }
  1550. score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, /*ref_index*/ 0, last_mv,
  1551. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  1552. assert(mx >= c->xmin);
  1553. assert(mx <= c->xmax);
  1554. assert(my >= c->ymin);
  1555. assert(my <= c->ymax);
  1556. score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  1557. score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  1558. //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2
  1559. // subpel search
  1560. pc= s->c;
  1561. init_put_bits(&pc.pb, p_buffer, sizeof(p_buffer));
  1562. memcpy(p_state, s->block_state, sizeof(s->block_state));
  1563. if(level!=s->block_max_depth)
  1564. put_cabac(&pc, &p_state[4 + s_context], 1);
  1565. put_cabac(&pc, &p_state[1 + left->type + top->type], 0);
  1566. put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1);
  1567. put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1);
  1568. p_len= put_bits_count(&pc.pb);
  1569. score += (s->lambda2*(p_len + pc.outstanding_count - s->c.outstanding_count))>>FF_LAMBDA_SHIFT;
  1570. block_s= block_w*block_w;
  1571. sum = pix_sum(&current_mb[0][0], stride, block_w);
  1572. l= (sum + block_s/2)/block_s;
  1573. iscore = pix_norm1(&current_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s;
  1574. block_s= block_w*block_w>>2;
  1575. sum = pix_sum(&current_mb[1][0], uvstride, block_w>>1);
  1576. cb= (sum + block_s/2)/block_s;
  1577. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  1578. sum = pix_sum(&current_mb[2][0], uvstride, block_w>>1);
  1579. cr= (sum + block_s/2)/block_s;
  1580. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  1581. ic= s->c;
  1582. init_put_bits(&ic.pb, i_buffer, sizeof(i_buffer));
  1583. memcpy(i_state, s->block_state, sizeof(s->block_state));
  1584. if(level!=s->block_max_depth)
  1585. put_cabac(&ic, &i_state[4 + s_context], 1);
  1586. put_cabac(&ic, &i_state[1 + left->type + top->type], 1);
  1587. put_symbol(&ic, &i_state[32], l-pl , 1);
  1588. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  1589. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  1590. i_len= put_bits_count(&ic.pb);
  1591. iscore += (s->lambda2*(i_len + ic.outstanding_count - s->c.outstanding_count))>>FF_LAMBDA_SHIFT;
  1592. // assert(score==256*256*256*64-1);
  1593. assert(iscore < 255*255*256 + s->lambda2*10);
  1594. assert(iscore >= 0);
  1595. assert(l>=0 && l<=255);
  1596. assert(pl>=0 && pl<=255);
  1597. if(level==0){
  1598. int varc= iscore >> 8;
  1599. int vard= score >> 8;
  1600. if (vard <= 64 || vard < varc)
  1601. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1602. else
  1603. c->scene_change_score+= s->m.qscale;
  1604. }
  1605. if(level!=s->block_max_depth){
  1606. put_cabac(&s->c, &s->block_state[4 + s_context], 0);
  1607. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1608. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1609. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1610. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1611. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  1612. if(score2 < score && score2 < iscore)
  1613. return score2;
  1614. }
  1615. if(iscore < score){
  1616. flush_put_bits(&ic.pb);
  1617. ff_copy_bits(&pbbak, i_buffer, i_len);
  1618. s->c= ic;
  1619. s->c.pb= pbbak;
  1620. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA);
  1621. memcpy(s->block_state, i_state, sizeof(s->block_state));
  1622. return iscore;
  1623. }else{
  1624. flush_put_bits(&pc.pb);
  1625. ff_copy_bits(&pbbak, p_buffer, p_len);
  1626. s->c= pc;
  1627. s->c.pb= pbbak;
  1628. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0);
  1629. memcpy(s->block_state, p_state, sizeof(s->block_state));
  1630. return score;
  1631. }
  1632. }
  1633. static void decode_q_branch(SnowContext *s, int level, int x, int y){
  1634. const int w= s->b_width << s->block_max_depth;
  1635. const int rem_depth= s->block_max_depth - level;
  1636. const int index= (x + y*w) << rem_depth;
  1637. static BlockNode null_block= { //FIXME add border maybe
  1638. .color= {128,128,128},
  1639. .mx= 0,
  1640. .my= 0,
  1641. .type= 0,
  1642. .level= 0,
  1643. };
  1644. int trx= (x+1)<<rem_depth;
  1645. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1646. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1647. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1648. BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1649. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1650. if(s->keyframe){
  1651. 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);
  1652. return;
  1653. }
  1654. if(level==s->block_max_depth || get_cabac(&s->c, &s->block_state[4 + s_context])){
  1655. int type;
  1656. int l = left->color[0];
  1657. int cb= left->color[1];
  1658. int cr= left->color[2];
  1659. int mx= mid_pred(left->mx, top->mx, tr->mx);
  1660. int my= mid_pred(left->my, top->my, tr->my);
  1661. int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx));
  1662. int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my));
  1663. type= get_cabac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  1664. if(type){
  1665. l += get_symbol(&s->c, &s->block_state[32], 1);
  1666. cb+= get_symbol(&s->c, &s->block_state[64], 1);
  1667. cr+= get_symbol(&s->c, &s->block_state[96], 1);
  1668. }else{
  1669. mx+= get_symbol(&s->c, &s->block_state[128 + 32*mx_context], 1);
  1670. my+= get_symbol(&s->c, &s->block_state[128 + 32*my_context], 1);
  1671. }
  1672. set_blocks(s, level, x, y, l, cb, cr, mx, my, type);
  1673. }else{
  1674. decode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1675. decode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1676. decode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1677. decode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1678. }
  1679. }
  1680. static void encode_blocks(SnowContext *s){
  1681. int x, y;
  1682. int w= s->b_width;
  1683. int h= s->b_height;
  1684. for(y=0; y<h; y++){
  1685. for(x=0; x<w; x++){
  1686. encode_q_branch(s, 0, x, y);
  1687. }
  1688. }
  1689. }
  1690. static void decode_blocks(SnowContext *s){
  1691. int x, y;
  1692. int w= s->b_width;
  1693. int h= s->b_height;
  1694. for(y=0; y<h; y++){
  1695. for(x=0; x<w; x++){
  1696. decode_q_branch(s, 0, x, y);
  1697. }
  1698. }
  1699. }
  1700. 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){
  1701. int x, y;
  1702. START_TIMER
  1703. for(y=0; y < b_h+5; y++){
  1704. for(x=0; x < b_w; x++){
  1705. int a0= src[x ];
  1706. int a1= src[x + 1];
  1707. int a2= src[x + 2];
  1708. int a3= src[x + 3];
  1709. int a4= src[x + 4];
  1710. int a5= src[x + 5];
  1711. // int am= 9*(a1+a2) - (a0+a3);
  1712. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  1713. // int am= 18*(a2+a3) - 2*(a1+a4);
  1714. // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  1715. // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
  1716. // if(b_w==16) am= 8*(a1+a2);
  1717. if(dx<8) tmp[x]= (32*a2*( 8-dx) + am* dx + 128)>>8;
  1718. else tmp[x]= ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
  1719. /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
  1720. else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
  1721. else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
  1722. else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
  1723. }
  1724. tmp += stride;
  1725. src += stride;
  1726. }
  1727. tmp -= (b_h+5)*stride;
  1728. for(y=0; y < b_h; y++){
  1729. for(x=0; x < b_w; x++){
  1730. int a0= tmp[x + 0*stride];
  1731. int a1= tmp[x + 1*stride];
  1732. int a2= tmp[x + 2*stride];
  1733. int a3= tmp[x + 3*stride];
  1734. int a4= tmp[x + 4*stride];
  1735. int a5= tmp[x + 5*stride];
  1736. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  1737. // int am= 18*(a2+a3) - 2*(a1+a4);
  1738. /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  1739. int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
  1740. // if(b_w==16) am= 8*(a1+a2);
  1741. if(dy<8) dst[x]= (32*a2*( 8-dy) + am* dy + 128)>>8;
  1742. else dst[x]= ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
  1743. /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
  1744. else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
  1745. else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
  1746. else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
  1747. }
  1748. dst += stride;
  1749. tmp += stride;
  1750. }
  1751. STOP_TIMER("mc_block")
  1752. }
  1753. #define mca(dx,dy,b_w)\
  1754. static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, uint8_t *src, int stride, int h){\
  1755. uint8_t tmp[stride*(b_w+5)];\
  1756. assert(h==b_w);\
  1757. mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
  1758. }
  1759. mca( 0, 0,16)
  1760. mca( 8, 0,16)
  1761. mca( 0, 8,16)
  1762. mca( 8, 8,16)
  1763. mca( 0, 0,8)
  1764. mca( 8, 0,8)
  1765. mca( 0, 8,8)
  1766. mca( 8, 8,8)
  1767. 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){
  1768. if(block->type){
  1769. int x, y;
  1770. const int color= block->color[plane_index];
  1771. for(y=0; y < b_h; y++){
  1772. for(x=0; x < b_w; x++){
  1773. dst[x + y*stride]= color;
  1774. }
  1775. }
  1776. }else{
  1777. const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
  1778. int mx= block->mx*scale;
  1779. int my= block->my*scale;
  1780. const int dx= mx&15;
  1781. const int dy= my&15;
  1782. sx += (mx>>4) - 2;
  1783. sy += (my>>4) - 2;
  1784. src += sx + sy*stride;
  1785. if( (unsigned)sx >= w - b_w - 4
  1786. || (unsigned)sy >= h - b_h - 4){
  1787. ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+5, b_h+5, sx, sy, w, h);
  1788. src= tmp + MB_SIZE;
  1789. }
  1790. if((dx&3) || (dy&3) || b_w!=b_h || (b_w!=4 && b_w!=8 && b_w!=16))
  1791. mc_block(dst, src, tmp, stride, b_w, b_h, dx, dy);
  1792. else
  1793. s->dsp.put_h264_qpel_pixels_tab[2-(b_w>>3)][dy+(dx>>2)](dst,src + 2 + 2*stride,stride);
  1794. }
  1795. }
  1796. static always_inline int same_block(BlockNode *a, BlockNode *b){
  1797. return !((a->mx - b->mx) | (a->my - b->my) | a->type | b->type);
  1798. }
  1799. //FIXME name clenup (b_w, block_w, b_width stuff)
  1800. 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){
  1801. const int b_width = s->b_width << s->block_max_depth;
  1802. const int b_height= s->b_height << s->block_max_depth;
  1803. const int b_stride= b_width;
  1804. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  1805. BlockNode *rt= lt+1;
  1806. BlockNode *lb= lt+b_stride;
  1807. BlockNode *rb= lb+1;
  1808. uint8_t *block[4];
  1809. uint8_t tmp[src_stride*(b_h+5)]; //FIXME align
  1810. int x,y;
  1811. if(b_x<0){
  1812. lt= rt;
  1813. lb= rb;
  1814. }else if(b_x + 1 >= b_width){
  1815. rt= lt;
  1816. rb= lb;
  1817. }
  1818. if(b_y<0){
  1819. lt= lb;
  1820. rt= rb;
  1821. }else if(b_y + 1 >= b_height){
  1822. lb= lt;
  1823. rb= rt;
  1824. }
  1825. if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
  1826. obmc -= src_x;
  1827. b_w += src_x;
  1828. src_x=0;
  1829. }else if(src_x + b_w > w){
  1830. b_w = w - src_x;
  1831. }
  1832. if(src_y<0){
  1833. obmc -= src_y*obmc_stride;
  1834. b_h += src_y;
  1835. src_y=0;
  1836. }else if(src_y + b_h> h){
  1837. b_h = h - src_y;
  1838. }
  1839. if(b_w<=0 || b_h<=0) return;
  1840. assert(src_stride > 7*MB_SIZE);
  1841. dst += src_x + src_y*dst_stride;
  1842. dst8+= src_x + src_y*src_stride;
  1843. // src += src_x + src_y*src_stride;
  1844. block[0]= tmp+3*MB_SIZE;
  1845. pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  1846. if(same_block(lt, rt)){
  1847. block[1]= block[0];
  1848. }else{
  1849. block[1]= tmp + 4*MB_SIZE;
  1850. pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  1851. }
  1852. if(same_block(lt, lb)){
  1853. block[2]= block[0];
  1854. }else if(same_block(rt, lb)){
  1855. block[2]= block[1];
  1856. }else{
  1857. block[2]= tmp+5*MB_SIZE;
  1858. pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  1859. }
  1860. if(same_block(lt, rb) ){
  1861. block[3]= block[0];
  1862. }else if(same_block(rt, rb)){
  1863. block[3]= block[1];
  1864. }else if(same_block(lb, rb)){
  1865. block[3]= block[2];
  1866. }else{
  1867. block[3]= tmp+6*MB_SIZE;
  1868. pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  1869. }
  1870. #if 0
  1871. for(y=0; y<b_h; y++){
  1872. for(x=0; x<b_w; x++){
  1873. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  1874. if(add) dst[x + y*dst_stride] += v;
  1875. else dst[x + y*dst_stride] -= v;
  1876. }
  1877. }
  1878. for(y=0; y<b_h; y++){
  1879. uint8_t *obmc2= obmc + (obmc_stride>>1);
  1880. for(x=0; x<b_w; x++){
  1881. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  1882. if(add) dst[x + y*dst_stride] += v;
  1883. else dst[x + y*dst_stride] -= v;
  1884. }
  1885. }
  1886. for(y=0; y<b_h; y++){
  1887. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  1888. for(x=0; x<b_w; x++){
  1889. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  1890. if(add) dst[x + y*dst_stride] += v;
  1891. else dst[x + y*dst_stride] -= v;
  1892. }
  1893. }
  1894. for(y=0; y<b_h; y++){
  1895. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  1896. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  1897. for(x=0; x<b_w; x++){
  1898. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  1899. if(add) dst[x + y*dst_stride] += v;
  1900. else dst[x + y*dst_stride] -= v;
  1901. }
  1902. }
  1903. #else
  1904. for(y=0; y<b_h; y++){
  1905. //FIXME ugly missue of obmc_stride
  1906. uint8_t *obmc1= obmc + y*obmc_stride;
  1907. uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  1908. uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  1909. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  1910. for(x=0; x<b_w; x++){
  1911. int v= obmc1[x] * block[3][x + y*src_stride]
  1912. +obmc2[x] * block[2][x + y*src_stride]
  1913. +obmc3[x] * block[1][x + y*src_stride]
  1914. +obmc4[x] * block[0][x + y*src_stride];
  1915. v <<= 8 - LOG2_OBMC_MAX;
  1916. if(FRAC_BITS != 8){
  1917. v += 1<<(7 - FRAC_BITS);
  1918. v >>= 8 - FRAC_BITS;
  1919. }
  1920. if(add){
  1921. v += dst[x + y*dst_stride];
  1922. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  1923. if(v&(~255)) v= ~(v>>31);
  1924. dst8[x + y*src_stride] = v;
  1925. }else{
  1926. dst[x + y*dst_stride] -= v;
  1927. }
  1928. }
  1929. }
  1930. #endif
  1931. }
  1932. static always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
  1933. Plane *p= &s->plane[plane_index];
  1934. const int mb_w= s->b_width << s->block_max_depth;
  1935. const int mb_h= s->b_height << s->block_max_depth;
  1936. int x, y, mb_x, mb_y;
  1937. int block_size = MB_SIZE >> s->block_max_depth;
  1938. int block_w = plane_index ? block_size/2 : block_size;
  1939. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  1940. int obmc_stride= plane_index ? block_size : 2*block_size;
  1941. int ref_stride= s->current_picture.linesize[plane_index];
  1942. uint8_t *ref = s->last_picture.data[plane_index];
  1943. uint8_t *dst8= s->current_picture.data[plane_index];
  1944. int w= p->width;
  1945. int h= p->height;
  1946. START_TIMER
  1947. if(s->keyframe || (s->avctx->debug&512)){
  1948. if(add){
  1949. for(y=0; y<h; y++){
  1950. for(x=0; x<w; x++){
  1951. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  1952. v >>= FRAC_BITS;
  1953. if(v&(~255)) v= ~(v>>31);
  1954. dst8[x + y*ref_stride]= v;
  1955. }
  1956. }
  1957. }else{
  1958. for(y=0; y<h; y++){
  1959. for(x=0; x<w; x++){
  1960. buf[x + y*w]-= 128<<FRAC_BITS;
  1961. }
  1962. }
  1963. }
  1964. return;
  1965. }
  1966. for(mb_y=0; mb_y<=mb_h; mb_y++){
  1967. for(mb_x=0; mb_x<=mb_w; mb_x++){
  1968. START_TIMER
  1969. add_yblock(s, buf, dst8, ref, obmc,
  1970. block_w*mb_x - block_w/2,
  1971. block_w*mb_y - block_w/2,
  1972. block_w, block_w,
  1973. w, h,
  1974. w, ref_stride, obmc_stride,
  1975. mb_x - 1, mb_y - 1,
  1976. add, plane_index);
  1977. STOP_TIMER("add_yblock")
  1978. }
  1979. }
  1980. STOP_TIMER("predict_plane")
  1981. }
  1982. static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
  1983. const int level= b->level;
  1984. const int w= b->width;
  1985. const int h= b->height;
  1986. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  1987. const int qmul= qexp[qlog&7]<<(qlog>>3);
  1988. int x,y, thres1, thres2;
  1989. START_TIMER
  1990. assert(QROOT==8);
  1991. if(s->qlog == LOSSLESS_QLOG) return;
  1992. bias= bias ? 0 : (3*qmul)>>3;
  1993. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  1994. thres2= 2*thres1;
  1995. if(!bias){
  1996. for(y=0; y<h; y++){
  1997. for(x=0; x<w; x++){
  1998. int i= src[x + y*stride];
  1999. if((unsigned)(i+thres1) > thres2){
  2000. if(i>=0){
  2001. i<<= QEXPSHIFT;
  2002. i/= qmul; //FIXME optimize
  2003. src[x + y*stride]= i;
  2004. }else{
  2005. i= -i;
  2006. i<<= QEXPSHIFT;
  2007. i/= qmul; //FIXME optimize
  2008. src[x + y*stride]= -i;
  2009. }
  2010. }else
  2011. src[x + y*stride]= 0;
  2012. }
  2013. }
  2014. }else{
  2015. for(y=0; y<h; y++){
  2016. for(x=0; x<w; x++){
  2017. int i= src[x + y*stride];
  2018. if((unsigned)(i+thres1) > thres2){
  2019. if(i>=0){
  2020. i<<= QEXPSHIFT;
  2021. i= (i + bias) / qmul; //FIXME optimize
  2022. src[x + y*stride]= i;
  2023. }else{
  2024. i= -i;
  2025. i<<= QEXPSHIFT;
  2026. i= (i + bias) / qmul; //FIXME optimize
  2027. src[x + y*stride]= -i;
  2028. }
  2029. }else
  2030. src[x + y*stride]= 0;
  2031. }
  2032. }
  2033. }
  2034. if(level+1 == s->spatial_decomposition_count){
  2035. // STOP_TIMER("quantize")
  2036. }
  2037. }
  2038. static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
  2039. const int w= b->width;
  2040. const int h= b->height;
  2041. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  2042. const int qmul= qexp[qlog&7]<<(qlog>>3);
  2043. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2044. int x,y;
  2045. START_TIMER
  2046. if(s->qlog == LOSSLESS_QLOG) return;
  2047. assert(QROOT==8);
  2048. for(y=0; y<h; y++){
  2049. for(x=0; x<w; x++){
  2050. int i= src[x + y*stride];
  2051. if(i<0){
  2052. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2053. }else if(i>0){
  2054. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2055. }
  2056. }
  2057. }
  2058. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  2059. STOP_TIMER("dquant")
  2060. }
  2061. }
  2062. static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  2063. const int w= b->width;
  2064. const int h= b->height;
  2065. int x,y;
  2066. for(y=h-1; y>=0; y--){
  2067. for(x=w-1; x>=0; x--){
  2068. int i= x + y*stride;
  2069. if(x){
  2070. if(use_median){
  2071. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2072. else src[i] -= src[i - 1];
  2073. }else{
  2074. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2075. else src[i] -= src[i - 1];
  2076. }
  2077. }else{
  2078. if(y) src[i] -= src[i - stride];
  2079. }
  2080. }
  2081. }
  2082. }
  2083. static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  2084. const int w= b->width;
  2085. const int h= b->height;
  2086. int x,y;
  2087. for(y=0; y<h; y++){
  2088. for(x=0; x<w; x++){
  2089. int i= x + y*stride;
  2090. if(x){
  2091. if(use_median){
  2092. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2093. else src[i] += src[i - 1];
  2094. }else{
  2095. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2096. else src[i] += src[i - 1];
  2097. }
  2098. }else{
  2099. if(y) src[i] += src[i - stride];
  2100. }
  2101. }
  2102. }
  2103. }
  2104. static void encode_header(SnowContext *s){
  2105. int plane_index, level, orientation;
  2106. uint8_t kstate[32]={0};
  2107. put_cabac(&s->c, kstate, s->keyframe);
  2108. if(s->keyframe || s->always_reset)
  2109. reset_contexts(s);
  2110. if(s->keyframe){
  2111. put_symbol(&s->c, s->header_state, s->version, 0);
  2112. put_cabac(&s->c, s->header_state, s->always_reset);
  2113. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  2114. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  2115. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2116. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  2117. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  2118. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  2119. put_cabac(&s->c, s->header_state, s->spatial_scalability);
  2120. // put_cabac(&s->c, s->header_state, s->rate_scalability);
  2121. for(plane_index=0; plane_index<2; plane_index++){
  2122. for(level=0; level<s->spatial_decomposition_count; level++){
  2123. for(orientation=level ? 1:0; orientation<4; orientation++){
  2124. if(orientation==2) continue;
  2125. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  2126. }
  2127. }
  2128. }
  2129. }
  2130. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
  2131. put_symbol(&s->c, s->header_state, s->qlog, 1);
  2132. put_symbol(&s->c, s->header_state, s->mv_scale, 0);
  2133. put_symbol(&s->c, s->header_state, s->qbias, 1);
  2134. put_symbol(&s->c, s->header_state, s->block_max_depth, 0);
  2135. }
  2136. static int decode_header(SnowContext *s){
  2137. int plane_index, level, orientation;
  2138. uint8_t kstate[32]={0};
  2139. s->keyframe= get_cabac(&s->c, kstate);
  2140. if(s->keyframe || s->always_reset)
  2141. reset_contexts(s);
  2142. if(s->keyframe){
  2143. s->version= get_symbol(&s->c, s->header_state, 0);
  2144. if(s->version>0){
  2145. av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
  2146. return -1;
  2147. }
  2148. s->always_reset= get_cabac(&s->c, s->header_state);
  2149. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2150. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2151. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2152. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  2153. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  2154. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  2155. s->spatial_scalability= get_cabac(&s->c, s->header_state);
  2156. // s->rate_scalability= get_cabac(&s->c, s->header_state);
  2157. for(plane_index=0; plane_index<3; plane_index++){
  2158. for(level=0; level<s->spatial_decomposition_count; level++){
  2159. for(orientation=level ? 1:0; orientation<4; orientation++){
  2160. int q;
  2161. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  2162. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  2163. else q= get_symbol(&s->c, s->header_state, 1);
  2164. s->plane[plane_index].band[level][orientation].qlog= q;
  2165. }
  2166. }
  2167. }
  2168. }
  2169. s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2170. if(s->spatial_decomposition_type > 2){
  2171. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  2172. return -1;
  2173. }
  2174. s->qlog= get_symbol(&s->c, s->header_state, 1);
  2175. s->mv_scale= get_symbol(&s->c, s->header_state, 0);
  2176. s->qbias= get_symbol(&s->c, s->header_state, 1);
  2177. s->block_max_depth= get_symbol(&s->c, s->header_state, 0);
  2178. return 0;
  2179. }
  2180. static int common_init(AVCodecContext *avctx){
  2181. SnowContext *s = avctx->priv_data;
  2182. int width, height;
  2183. int level, orientation, plane_index, dec;
  2184. s->avctx= avctx;
  2185. dsputil_init(&s->dsp, avctx);
  2186. #define mcf(dx,dy)\
  2187. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  2188. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  2189. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  2190. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  2191. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  2192. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  2193. mcf( 0, 0)
  2194. mcf( 4, 0)
  2195. mcf( 8, 0)
  2196. mcf(12, 0)
  2197. mcf( 0, 4)
  2198. mcf( 4, 4)
  2199. mcf( 8, 4)
  2200. mcf(12, 4)
  2201. mcf( 0, 8)
  2202. mcf( 4, 8)
  2203. mcf( 8, 8)
  2204. mcf(12, 8)
  2205. mcf( 0,12)
  2206. mcf( 4,12)
  2207. mcf( 8,12)
  2208. mcf(12,12)
  2209. #define mcfh(dx,dy)\
  2210. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  2211. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  2212. mc_block_hpel ## dx ## dy ## 16;\
  2213. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  2214. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  2215. mc_block_hpel ## dx ## dy ## 8;
  2216. mcfh(0, 0)
  2217. mcfh(8, 0)
  2218. mcfh(0, 8)
  2219. mcfh(8, 8)
  2220. dec= s->spatial_decomposition_count= 5;
  2221. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  2222. s->chroma_h_shift= 1; //FIXME XXX
  2223. s->chroma_v_shift= 1;
  2224. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  2225. width= s->avctx->width;
  2226. height= s->avctx->height;
  2227. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
  2228. s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  2229. s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
  2230. for(plane_index=0; plane_index<3; plane_index++){
  2231. int w= s->avctx->width;
  2232. int h= s->avctx->height;
  2233. if(plane_index){
  2234. w>>= s->chroma_h_shift;
  2235. h>>= s->chroma_v_shift;
  2236. }
  2237. s->plane[plane_index].width = w;
  2238. s->plane[plane_index].height= h;
  2239. //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
  2240. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  2241. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2242. SubBand *b= &s->plane[plane_index].band[level][orientation];
  2243. b->buf= s->spatial_dwt_buffer;
  2244. b->level= level;
  2245. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  2246. b->width = (w + !(orientation&1))>>1;
  2247. b->height= (h + !(orientation>1))>>1;
  2248. if(orientation&1) b->buf += (w+1)>>1;
  2249. if(orientation>1) b->buf += b->stride>>1;
  2250. if(level)
  2251. b->parent= &s->plane[plane_index].band[level-1][orientation];
  2252. b->x = av_mallocz(((b->width+1) * b->height+1)*sizeof(int16_t));
  2253. b->coeff= av_mallocz(((b->width+1) * b->height+1)*sizeof(DWTELEM));
  2254. }
  2255. w= (w+1)>>1;
  2256. h= (h+1)>>1;
  2257. }
  2258. }
  2259. reset_contexts(s);
  2260. /*
  2261. width= s->width= avctx->width;
  2262. height= s->height= avctx->height;
  2263. assert(width && height);
  2264. */
  2265. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  2266. return 0;
  2267. }
  2268. static void calculate_vissual_weight(SnowContext *s, Plane *p){
  2269. int width = p->width;
  2270. int height= p->height;
  2271. int level, orientation, x, y;
  2272. for(level=0; level<s->spatial_decomposition_count; level++){
  2273. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2274. SubBand *b= &p->band[level][orientation];
  2275. DWTELEM *buf= b->buf;
  2276. int64_t error=0;
  2277. memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
  2278. buf[b->width/2 + b->height/2*b->stride]= 256*256;
  2279. ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2280. for(y=0; y<height; y++){
  2281. for(x=0; x<width; x++){
  2282. int64_t d= s->spatial_dwt_buffer[x + y*width];
  2283. error += d*d;
  2284. }
  2285. }
  2286. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  2287. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
  2288. }
  2289. }
  2290. }
  2291. static int encode_init(AVCodecContext *avctx)
  2292. {
  2293. SnowContext *s = avctx->priv_data;
  2294. int plane_index;
  2295. if(avctx->strict_std_compliance >= 0){
  2296. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  2297. "use vstrict=-1 to use it anyway\n");
  2298. return -1;
  2299. }
  2300. common_init(avctx);
  2301. alloc_blocks(s);
  2302. s->version=0;
  2303. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  2304. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2305. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2306. h263_encode_init(&s->m); //mv_penalty
  2307. for(plane_index=0; plane_index<3; plane_index++){
  2308. calculate_vissual_weight(s, &s->plane[plane_index]);
  2309. }
  2310. avctx->coded_frame= &s->current_picture;
  2311. switch(avctx->pix_fmt){
  2312. // case PIX_FMT_YUV444P:
  2313. // case PIX_FMT_YUV422P:
  2314. case PIX_FMT_YUV420P:
  2315. case PIX_FMT_GRAY8:
  2316. // case PIX_FMT_YUV411P:
  2317. // case PIX_FMT_YUV410P:
  2318. s->colorspace_type= 0;
  2319. break;
  2320. /* case PIX_FMT_RGBA32:
  2321. s->colorspace= 1;
  2322. break;*/
  2323. default:
  2324. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  2325. return -1;
  2326. }
  2327. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  2328. s->chroma_h_shift= 1;
  2329. s->chroma_v_shift= 1;
  2330. return 0;
  2331. }
  2332. static int frame_start(SnowContext *s){
  2333. AVFrame tmp;
  2334. int w= s->avctx->width; //FIXME round up to x16 ?
  2335. int h= s->avctx->height;
  2336. if(s->current_picture.data[0]){
  2337. draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  2338. draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  2339. draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  2340. }
  2341. tmp= s->last_picture;
  2342. s->last_picture= s->current_picture;
  2343. s->current_picture= tmp;
  2344. s->current_picture.reference= 1;
  2345. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  2346. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  2347. return -1;
  2348. }
  2349. return 0;
  2350. }
  2351. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  2352. SnowContext *s = avctx->priv_data;
  2353. CABACContext * const c= &s->c;
  2354. AVFrame *pict = data;
  2355. const int width= s->avctx->width;
  2356. const int height= s->avctx->height;
  2357. int level, orientation, plane_index;
  2358. ff_init_cabac_encoder(c, buf, buf_size);
  2359. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2360. s->input_picture = *pict;
  2361. s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  2362. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  2363. if(pict->quality){
  2364. s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
  2365. //<64 >60
  2366. s->qlog += 61;
  2367. }else{
  2368. s->qlog= LOSSLESS_QLOG;
  2369. }
  2370. frame_start(s);
  2371. s->current_picture.key_frame= s->keyframe;
  2372. if(pict->pict_type == P_TYPE){
  2373. int block_width = (width +15)>>4;
  2374. int block_height= (height+15)>>4;
  2375. int stride= s->current_picture.linesize[0];
  2376. assert(s->current_picture.data[0]);
  2377. assert(s->last_picture.data[0]);
  2378. s->m.avctx= s->avctx;
  2379. s->m.current_picture.data[0]= s->current_picture.data[0];
  2380. s->m. last_picture.data[0]= s-> last_picture.data[0];
  2381. s->m. new_picture.data[0]= s-> input_picture.data[0];
  2382. s->m.current_picture_ptr= &s->m.current_picture;
  2383. s->m. last_picture_ptr= &s->m. last_picture;
  2384. s->m.linesize=
  2385. s->m. last_picture.linesize[0]=
  2386. s->m. new_picture.linesize[0]=
  2387. s->m.current_picture.linesize[0]= stride;
  2388. s->m.uvlinesize= s->current_picture.linesize[1];
  2389. s->m.width = width;
  2390. s->m.height= height;
  2391. s->m.mb_width = block_width;
  2392. s->m.mb_height= block_height;
  2393. s->m.mb_stride= s->m.mb_width+1;
  2394. s->m.b8_stride= 2*s->m.mb_width+1;
  2395. s->m.f_code=1;
  2396. s->m.pict_type= pict->pict_type;
  2397. s->m.me_method= s->avctx->me_method;
  2398. s->m.me.scene_change_score=0;
  2399. s->m.flags= s->avctx->flags;
  2400. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  2401. s->m.out_format= FMT_H263;
  2402. s->m.unrestricted_mv= 1;
  2403. s->lambda = s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else
  2404. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  2405. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  2406. s->m.dsp= s->dsp; //move
  2407. ff_init_me(&s->m);
  2408. }
  2409. redo_frame:
  2410. s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
  2411. encode_header(s);
  2412. encode_blocks(s);
  2413. for(plane_index=0; plane_index<3; plane_index++){
  2414. Plane *p= &s->plane[plane_index];
  2415. int w= p->width;
  2416. int h= p->height;
  2417. int x, y;
  2418. // int bits= put_bits_count(&s->c.pb);
  2419. //FIXME optimize
  2420. if(pict->data[plane_index]) //FIXME gray hack
  2421. for(y=0; y<h; y++){
  2422. for(x=0; x<w; x++){
  2423. s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  2424. }
  2425. }
  2426. predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
  2427. if( plane_index==0
  2428. && pict->pict_type == P_TYPE
  2429. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  2430. ff_init_cabac_encoder(c, buf, buf_size);
  2431. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2432. pict->pict_type= FF_I_TYPE;
  2433. s->keyframe=1;
  2434. reset_contexts(s);
  2435. goto redo_frame;
  2436. }
  2437. if(s->qlog == LOSSLESS_QLOG){
  2438. for(y=0; y<h; y++){
  2439. for(x=0; x<w; x++){
  2440. s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1)))>>FRAC_BITS;
  2441. }
  2442. }
  2443. }
  2444. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2445. for(level=0; level<s->spatial_decomposition_count; level++){
  2446. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2447. SubBand *b= &p->band[level][orientation];
  2448. quantize(s, b, b->buf, b->stride, s->qbias);
  2449. if(orientation==0)
  2450. decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
  2451. encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  2452. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  2453. if(orientation==0)
  2454. correlate(s, b, b->buf, b->stride, 1, 0);
  2455. }
  2456. }
  2457. // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
  2458. for(level=0; level<s->spatial_decomposition_count; level++){
  2459. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2460. SubBand *b= &p->band[level][orientation];
  2461. dequantize(s, b, b->buf, b->stride);
  2462. }
  2463. }
  2464. ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2465. if(s->qlog == LOSSLESS_QLOG){
  2466. for(y=0; y<h; y++){
  2467. for(x=0; x<w; x++){
  2468. s->spatial_dwt_buffer[y*w + x]<<=FRAC_BITS;
  2469. }
  2470. }
  2471. }
  2472. {START_TIMER
  2473. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2474. STOP_TIMER("pred-conv")}
  2475. if(s->avctx->flags&CODEC_FLAG_PSNR){
  2476. int64_t error= 0;
  2477. if(pict->data[plane_index]) //FIXME gray hack
  2478. for(y=0; y<h; y++){
  2479. for(x=0; x<w; x++){
  2480. 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];
  2481. error += d*d;
  2482. }
  2483. }
  2484. s->avctx->error[plane_index] += error;
  2485. s->current_picture.error[plane_index] = error;
  2486. }
  2487. }
  2488. if(s->last_picture.data[0])
  2489. avctx->release_buffer(avctx, &s->last_picture);
  2490. emms_c();
  2491. return put_cabac_terminate(c, 1);
  2492. }
  2493. static void common_end(SnowContext *s){
  2494. int plane_index, level, orientation;
  2495. av_freep(&s->spatial_dwt_buffer);
  2496. av_freep(&s->m.me.scratchpad);
  2497. av_freep(&s->m.me.map);
  2498. av_freep(&s->m.me.score_map);
  2499. av_freep(&s->block);
  2500. for(plane_index=0; plane_index<3; plane_index++){
  2501. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  2502. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2503. SubBand *b= &s->plane[plane_index].band[level][orientation];
  2504. av_freep(&b->x);
  2505. av_freep(&b->coeff);
  2506. }
  2507. }
  2508. }
  2509. }
  2510. static int encode_end(AVCodecContext *avctx)
  2511. {
  2512. SnowContext *s = avctx->priv_data;
  2513. common_end(s);
  2514. return 0;
  2515. }
  2516. static int decode_init(AVCodecContext *avctx)
  2517. {
  2518. // SnowContext *s = avctx->priv_data;
  2519. common_init(avctx);
  2520. return 0;
  2521. }
  2522. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  2523. SnowContext *s = avctx->priv_data;
  2524. CABACContext * const c= &s->c;
  2525. int bytes_read;
  2526. AVFrame *picture = data;
  2527. int level, orientation, plane_index;
  2528. /* no supplementary picture */
  2529. if (buf_size == 0)
  2530. return 0;
  2531. ff_init_cabac_decoder(c, buf, buf_size);
  2532. ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2533. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  2534. decode_header(s);
  2535. if(!s->block) alloc_blocks(s);
  2536. frame_start(s);
  2537. //keyframe flag dupliaction mess FIXME
  2538. if(avctx->debug&FF_DEBUG_PICT_INFO)
  2539. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  2540. decode_blocks(s);
  2541. for(plane_index=0; plane_index<3; plane_index++){
  2542. Plane *p= &s->plane[plane_index];
  2543. int w= p->width;
  2544. int h= p->height;
  2545. int x, y;
  2546. if(s->avctx->debug&2048){
  2547. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  2548. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2549. for(y=0; y<h; y++){
  2550. for(x=0; x<w; x++){
  2551. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  2552. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  2553. }
  2554. }
  2555. }
  2556. for(level=0; level<s->spatial_decomposition_count; level++){
  2557. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2558. SubBand *b= &p->band[level][orientation];
  2559. decode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  2560. if(orientation==0){
  2561. correlate(s, b, b->buf, b->stride, 1, 0);
  2562. dequantize(s, b, b->buf, b->stride);
  2563. assert(b->buf == s->spatial_dwt_buffer);
  2564. }
  2565. }
  2566. }
  2567. ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2568. if(s->qlog == LOSSLESS_QLOG){
  2569. for(y=0; y<h; y++){
  2570. for(x=0; x<w; x++){
  2571. s->spatial_dwt_buffer[y*w + x]<<=FRAC_BITS;
  2572. }
  2573. }
  2574. }
  2575. {START_TIMER
  2576. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  2577. STOP_TIMER("predict_plane conv2")}
  2578. }
  2579. emms_c();
  2580. if(s->last_picture.data[0])
  2581. avctx->release_buffer(avctx, &s->last_picture);
  2582. if(!(s->avctx->debug&2048))
  2583. *picture= s->current_picture;
  2584. else
  2585. *picture= s->mconly_picture;
  2586. *data_size = sizeof(AVFrame);
  2587. bytes_read= get_cabac_terminate(c);
  2588. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n");
  2589. return bytes_read;
  2590. }
  2591. static int decode_end(AVCodecContext *avctx)
  2592. {
  2593. SnowContext *s = avctx->priv_data;
  2594. common_end(s);
  2595. return 0;
  2596. }
  2597. AVCodec snow_decoder = {
  2598. "snow",
  2599. CODEC_TYPE_VIDEO,
  2600. CODEC_ID_SNOW,
  2601. sizeof(SnowContext),
  2602. decode_init,
  2603. NULL,
  2604. decode_end,
  2605. decode_frame,
  2606. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  2607. NULL
  2608. };
  2609. AVCodec snow_encoder = {
  2610. "snow",
  2611. CODEC_TYPE_VIDEO,
  2612. CODEC_ID_SNOW,
  2613. sizeof(SnowContext),
  2614. encode_init,
  2615. encode_frame,
  2616. encode_end,
  2617. };
  2618. #if 0
  2619. #undef malloc
  2620. #undef free
  2621. #undef printf
  2622. int main(){
  2623. int width=256;
  2624. int height=256;
  2625. int buffer[2][width*height];
  2626. SnowContext s;
  2627. int i;
  2628. s.spatial_decomposition_count=6;
  2629. s.spatial_decomposition_type=1;
  2630. printf("testing 5/3 DWT\n");
  2631. for(i=0; i<width*height; i++)
  2632. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  2633. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2634. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2635. for(i=0; i<width*height; i++)
  2636. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  2637. printf("testing 9/7 DWT\n");
  2638. s.spatial_decomposition_type=0;
  2639. for(i=0; i<width*height; i++)
  2640. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  2641. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2642. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2643. for(i=0; i<width*height; i++)
  2644. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  2645. printf("testing AC coder\n");
  2646. memset(s.header_state, 0, sizeof(s.header_state));
  2647. ff_init_cabac_encoder(&s.c, buffer[0], 256*256);
  2648. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2649. for(i=-256; i<256; i++){
  2650. START_TIMER
  2651. put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
  2652. STOP_TIMER("put_symbol")
  2653. }
  2654. put_cabac_terminate(&s.c, 1);
  2655. memset(s.header_state, 0, sizeof(s.header_state));
  2656. ff_init_cabac_decoder(&s.c, buffer[0], 256*256);
  2657. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  2658. for(i=-256; i<256; i++){
  2659. int j;
  2660. START_TIMER
  2661. j= get_symbol(&s.c, s.header_state, 1);
  2662. STOP_TIMER("get_symbol")
  2663. if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
  2664. }
  2665. {
  2666. int level, orientation, x, y;
  2667. int64_t errors[8][4];
  2668. int64_t g=0;
  2669. memset(errors, 0, sizeof(errors));
  2670. s.spatial_decomposition_count=3;
  2671. s.spatial_decomposition_type=0;
  2672. for(level=0; level<s.spatial_decomposition_count; level++){
  2673. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2674. int w= width >> (s.spatial_decomposition_count-level);
  2675. int h= height >> (s.spatial_decomposition_count-level);
  2676. int stride= width << (s.spatial_decomposition_count-level);
  2677. DWTELEM *buf= buffer[0];
  2678. int64_t error=0;
  2679. if(orientation&1) buf+=w;
  2680. if(orientation>1) buf+=stride>>1;
  2681. memset(buffer[0], 0, sizeof(int)*width*height);
  2682. buf[w/2 + h/2*stride]= 256*256;
  2683. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2684. for(y=0; y<height; y++){
  2685. for(x=0; x<width; x++){
  2686. int64_t d= buffer[0][x + y*width];
  2687. error += d*d;
  2688. if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
  2689. }
  2690. if(ABS(height/2-y)<9 && level==2) printf("\n");
  2691. }
  2692. error= (int)(sqrt(error)+0.5);
  2693. errors[level][orientation]= error;
  2694. if(g) g=ff_gcd(g, error);
  2695. else g= error;
  2696. }
  2697. }
  2698. printf("static int const visual_weight[][4]={\n");
  2699. for(level=0; level<s.spatial_decomposition_count; level++){
  2700. printf(" {");
  2701. for(orientation=0; orientation<4; orientation++){
  2702. printf("%8lld,", errors[level][orientation]/g);
  2703. }
  2704. printf("},\n");
  2705. }
  2706. printf("};\n");
  2707. {
  2708. int level=2;
  2709. int orientation=3;
  2710. int w= width >> (s.spatial_decomposition_count-level);
  2711. int h= height >> (s.spatial_decomposition_count-level);
  2712. int stride= width << (s.spatial_decomposition_count-level);
  2713. DWTELEM *buf= buffer[0];
  2714. int64_t error=0;
  2715. buf+=w;
  2716. buf+=stride>>1;
  2717. memset(buffer[0], 0, sizeof(int)*width*height);
  2718. #if 1
  2719. for(y=0; y<height; y++){
  2720. for(x=0; x<width; x++){
  2721. int tab[4]={0,2,3,1};
  2722. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  2723. }
  2724. }
  2725. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2726. #else
  2727. for(y=0; y<h; y++){
  2728. for(x=0; x<w; x++){
  2729. buf[x + y*stride ]=169;
  2730. buf[x + y*stride-w]=64;
  2731. }
  2732. }
  2733. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2734. #endif
  2735. for(y=0; y<height; y++){
  2736. for(x=0; x<width; x++){
  2737. int64_t d= buffer[0][x + y*width];
  2738. error += d*d;
  2739. if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
  2740. }
  2741. if(ABS(height/2-y)<9) printf("\n");
  2742. }
  2743. }
  2744. }
  2745. return 0;
  2746. }
  2747. #endif