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.

3173 lines
106KB

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