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.

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