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.

3145 lines
105KB

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