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.

3293 lines
113KB

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