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.

3259 lines
109KB

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