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.

3881 lines
131KB

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