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.

3876 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 spatial_compose97i_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int height, int stride_line){
  1240. cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
  1241. cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
  1242. cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
  1243. cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
  1244. cs->y = -3;
  1245. }
  1246. static void spatial_compose97i_init(dwt_compose_t *cs, DWTELEM *buffer, int height, int stride){
  1247. cs->b0 = buffer + mirror(-3-1, height-1)*stride;
  1248. cs->b1 = buffer + mirror(-3 , height-1)*stride;
  1249. cs->b2 = buffer + mirror(-3+1, height-1)*stride;
  1250. cs->b3 = buffer + mirror(-3+2, height-1)*stride;
  1251. cs->y = -3;
  1252. }
  1253. static void spatial_compose97i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
  1254. int y = cs->y;
  1255. int mirror0 = mirror(y - 1, height - 1);
  1256. int mirror1 = mirror(y + 0, height - 1);
  1257. int mirror2 = mirror(y + 1, height - 1);
  1258. int mirror3 = mirror(y + 2, height - 1);
  1259. int mirror4 = mirror(y + 3, height - 1);
  1260. int mirror5 = mirror(y + 4, height - 1);
  1261. DWTELEM *b0= cs->b0;
  1262. DWTELEM *b1= cs->b1;
  1263. DWTELEM *b2= cs->b2;
  1264. DWTELEM *b3= cs->b3;
  1265. DWTELEM *b4= slice_buffer_get_line(sb, mirror4 * stride_line);
  1266. DWTELEM *b5= slice_buffer_get_line(sb, mirror5 * stride_line);
  1267. if(stride_line == 1 && y+4 < height && 0){
  1268. int x;
  1269. for(x=0; x<width/2; x++)
  1270. b5[x] += 64*2;
  1271. for(; x<width; x++)
  1272. b5[x] += 169*2;
  1273. }
  1274. // if(mirror3 <= mirror5 && mirror2 <= mirror4 && mirror1 <= mirror3 && mirror0 <= mirror2)
  1275. // {
  1276. //{START_TIMER
  1277. // vertical_compose97_complete(b0, b1, b2, b3, b4, b5, width);
  1278. //if(width>400){
  1279. //STOP_TIMER("vertical_compose97i-NEW")}}
  1280. // }
  1281. // else
  1282. // {
  1283. {START_TIMER
  1284. if(mirror3 <= mirror5) vertical_compose97iL1(b3, b4, b5, width);
  1285. if(mirror2 <= mirror4) vertical_compose97iH1(b2, b3, b4, width);
  1286. if(mirror1 <= mirror3) vertical_compose97iL0(b1, b2, b3, width);
  1287. if(mirror0 <= mirror2) vertical_compose97iH0(b0, b1, b2, width);
  1288. if(width>400){
  1289. STOP_TIMER("vertical_compose97i")}}
  1290. // }
  1291. {START_TIMER
  1292. if(y-1>= 0) horizontal_compose97i(b0, width);
  1293. if(mirror0 <= mirror2) horizontal_compose97i(b1, width);
  1294. if(width>400 && mirror0 <= mirror2){
  1295. STOP_TIMER("horizontal_compose97i")}}
  1296. cs->b0=b2;
  1297. cs->b1=b3;
  1298. cs->b2=b4;
  1299. cs->b3=b5;
  1300. cs->y += 2;
  1301. }
  1302. static void spatial_compose97i_dy(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride){
  1303. int y = cs->y;
  1304. DWTELEM *b0= cs->b0;
  1305. DWTELEM *b1= cs->b1;
  1306. DWTELEM *b2= cs->b2;
  1307. DWTELEM *b3= cs->b3;
  1308. DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
  1309. DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
  1310. if(stride == width && y+4 < height && 0){
  1311. int x;
  1312. for(x=0; x<width/2; x++)
  1313. b5[x] += 64*2;
  1314. for(; x<width; x++)
  1315. b5[x] += 169*2;
  1316. }
  1317. {START_TIMER
  1318. if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
  1319. if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
  1320. if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
  1321. if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
  1322. if(width>400){
  1323. STOP_TIMER("vertical_compose97i")}}
  1324. {START_TIMER
  1325. if(y-1>= 0) horizontal_compose97i(b0, width);
  1326. if(b0 <= b2) horizontal_compose97i(b1, width);
  1327. if(width>400 && b0 <= b2){
  1328. STOP_TIMER("horizontal_compose97i")}}
  1329. cs->b0=b2;
  1330. cs->b1=b3;
  1331. cs->b2=b4;
  1332. cs->b3=b5;
  1333. cs->y += 2;
  1334. }
  1335. static void spatial_compose97i(DWTELEM *buffer, int width, int height, int stride){
  1336. dwt_compose_t cs;
  1337. spatial_compose97i_init(&cs, buffer, height, stride);
  1338. while(cs.y <= height)
  1339. spatial_compose97i_dy(&cs, buffer, width, height, stride);
  1340. }
  1341. 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){
  1342. int level;
  1343. for(level=decomposition_count-1; level>=0; level--){
  1344. switch(type){
  1345. case 0: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  1346. case 1: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
  1347. /* not slicified yet */
  1348. case 2: /*spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;*/
  1349. av_log(NULL, AV_LOG_ERROR, "spatial_composeX neither buffered nor slicified yet.\n"); break;
  1350. }
  1351. }
  1352. }
  1353. void ff_spatial_idwt_init(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  1354. int level;
  1355. for(level=decomposition_count-1; level>=0; level--){
  1356. switch(type){
  1357. case 0: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
  1358. case 1: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
  1359. /* not slicified yet */
  1360. case 2: spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;
  1361. }
  1362. }
  1363. }
  1364. void ff_spatial_idwt_slice(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
  1365. const int support = type==1 ? 3 : 5;
  1366. int level;
  1367. if(type==2) return;
  1368. for(level=decomposition_count-1; level>=0; level--){
  1369. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  1370. switch(type){
  1371. case 0: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  1372. break;
  1373. case 1: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
  1374. break;
  1375. case 2: break;
  1376. }
  1377. }
  1378. }
  1379. }
  1380. 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){
  1381. const int support = type==1 ? 3 : 5;
  1382. int level;
  1383. if(type==2) return;
  1384. for(level=decomposition_count-1; level>=0; level--){
  1385. while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
  1386. switch(type){
  1387. case 0: spatial_compose97i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  1388. break;
  1389. case 1: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
  1390. break;
  1391. case 2: break;
  1392. }
  1393. }
  1394. }
  1395. }
  1396. void ff_spatial_idwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
  1397. if(type==2){
  1398. int level;
  1399. for(level=decomposition_count-1; level>=0; level--)
  1400. spatial_composeX (buffer, width>>level, height>>level, stride<<level);
  1401. }else{
  1402. dwt_compose_t cs[MAX_DECOMPOSITIONS];
  1403. int y;
  1404. ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
  1405. for(y=0; y<height; y+=4)
  1406. ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
  1407. }
  1408. }
  1409. static int encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1410. const int w= b->width;
  1411. const int h= b->height;
  1412. int x, y;
  1413. if(1){
  1414. int run=0;
  1415. int runs[w*h];
  1416. int run_index=0;
  1417. for(y=0; y<h; y++){
  1418. for(x=0; x<w; x++){
  1419. int v, p=0;
  1420. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1421. v= src[x + y*stride];
  1422. if(y){
  1423. t= src[x + (y-1)*stride];
  1424. if(x){
  1425. lt= src[x - 1 + (y-1)*stride];
  1426. }
  1427. if(x + 1 < w){
  1428. rt= src[x + 1 + (y-1)*stride];
  1429. }
  1430. }
  1431. if(x){
  1432. l= src[x - 1 + y*stride];
  1433. /*if(x > 1){
  1434. if(orientation==1) ll= src[y + (x-2)*stride];
  1435. else ll= src[x - 2 + y*stride];
  1436. }*/
  1437. }
  1438. if(parent){
  1439. int px= x>>1;
  1440. int py= y>>1;
  1441. if(px<b->parent->width && py<b->parent->height)
  1442. p= parent[px + py*2*stride];
  1443. }
  1444. if(!(/*ll|*/l|lt|t|rt|p)){
  1445. if(v){
  1446. runs[run_index++]= run;
  1447. run=0;
  1448. }else{
  1449. run++;
  1450. }
  1451. }
  1452. }
  1453. }
  1454. runs[run_index++]= run;
  1455. run_index=0;
  1456. run= runs[run_index++];
  1457. put_symbol2(&s->c, b->state[1], run, 3);
  1458. for(y=0; y<h; y++){
  1459. if(s->c.bytestream_end - s->c.bytestream < w*40){
  1460. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1461. return -1;
  1462. }
  1463. for(x=0; x<w; x++){
  1464. int v, p=0;
  1465. int /*ll=0, */l=0, lt=0, t=0, rt=0;
  1466. v= src[x + y*stride];
  1467. if(y){
  1468. t= src[x + (y-1)*stride];
  1469. if(x){
  1470. lt= src[x - 1 + (y-1)*stride];
  1471. }
  1472. if(x + 1 < w){
  1473. rt= src[x + 1 + (y-1)*stride];
  1474. }
  1475. }
  1476. if(x){
  1477. l= src[x - 1 + y*stride];
  1478. /*if(x > 1){
  1479. if(orientation==1) ll= src[y + (x-2)*stride];
  1480. else ll= src[x - 2 + y*stride];
  1481. }*/
  1482. }
  1483. if(parent){
  1484. int px= x>>1;
  1485. int py= y>>1;
  1486. if(px<b->parent->width && py<b->parent->height)
  1487. p= parent[px + py*2*stride];
  1488. }
  1489. if(/*ll|*/l|lt|t|rt|p){
  1490. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1491. put_rac(&s->c, &b->state[0][context], !!v);
  1492. }else{
  1493. if(!run){
  1494. run= runs[run_index++];
  1495. put_symbol2(&s->c, b->state[1], run, 3);
  1496. assert(v);
  1497. }else{
  1498. run--;
  1499. assert(!v);
  1500. }
  1501. }
  1502. if(v){
  1503. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1504. put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
  1505. put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
  1506. }
  1507. }
  1508. }
  1509. }
  1510. return 0;
  1511. }
  1512. static int encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
  1513. // encode_subband_qtree(s, b, src, parent, stride, orientation);
  1514. // encode_subband_z0run(s, b, src, parent, stride, orientation);
  1515. return encode_subband_c0run(s, b, src, parent, stride, orientation);
  1516. // encode_subband_dzr(s, b, src, parent, stride, orientation);
  1517. }
  1518. static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
  1519. const int w= b->width;
  1520. const int h= b->height;
  1521. int x,y;
  1522. if(1){
  1523. int run;
  1524. int index=0;
  1525. int prev_index=-1;
  1526. int prev2_index=0;
  1527. int parent_index= 0;
  1528. int prev_parent_index= 0;
  1529. run= get_symbol2(&s->c, b->state[1], 3);
  1530. for(y=0; y<h; y++){
  1531. int v=0;
  1532. int lt=0, t=0, rt=0;
  1533. if(y && b->x_coeff[prev_index].x == 0){
  1534. rt= b->x_coeff[prev_index].coeff;
  1535. }
  1536. for(x=0; x<w; x++){
  1537. int p=0;
  1538. const int l= v;
  1539. lt= t; t= rt;
  1540. if(y){
  1541. if(b->x_coeff[prev_index].x <= x)
  1542. prev_index++;
  1543. if(b->x_coeff[prev_index].x == x + 1)
  1544. rt= b->x_coeff[prev_index].coeff;
  1545. else
  1546. rt=0;
  1547. }
  1548. if(parent){
  1549. if(x>>1 > parent->x_coeff[parent_index].x){
  1550. parent_index++;
  1551. }
  1552. if(x>>1 == parent->x_coeff[parent_index].x){
  1553. p= parent->x_coeff[parent_index].coeff;
  1554. }
  1555. }
  1556. if(/*ll|*/l|lt|t|rt|p){
  1557. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1558. v=get_rac(&s->c, &b->state[0][context]);
  1559. }else{
  1560. if(!run){
  1561. run= get_symbol2(&s->c, b->state[1], 3);
  1562. v=1;
  1563. }else{
  1564. run--;
  1565. v=0;
  1566. if(y && parent){
  1567. int max_run;
  1568. max_run= FFMIN(run, b->x_coeff[prev_index].x - x - 2);
  1569. max_run= FFMIN(max_run, 2*parent->x_coeff[parent_index].x - x - 1);
  1570. x+= max_run;
  1571. run-= max_run;
  1572. }
  1573. }
  1574. }
  1575. if(v){
  1576. int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
  1577. v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1;
  1578. if(get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
  1579. v *= -1;
  1580. b->x_coeff[index].x=x;
  1581. b->x_coeff[index++].coeff= v;
  1582. }
  1583. }
  1584. b->x_coeff[index++].x= w+1; //end marker
  1585. prev_index= prev2_index;
  1586. prev2_index= index;
  1587. if(parent){
  1588. if(y&1){
  1589. while(parent->x_coeff[parent_index].x != parent->width+1)
  1590. parent_index++;
  1591. parent_index++;
  1592. prev_parent_index= parent_index;
  1593. }else{
  1594. parent_index= prev_parent_index;
  1595. }
  1596. }
  1597. }
  1598. b->x_coeff[index++].x= w+1; //end marker
  1599. }
  1600. }
  1601. static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
  1602. const int w= b->width;
  1603. int x,y;
  1604. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  1605. int qmul= qexp[qlog&7]<<(qlog>>3);
  1606. int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  1607. int new_index = 0;
  1608. START_TIMER
  1609. if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){
  1610. qadd= 0;
  1611. qmul= 1<<QEXPSHIFT;
  1612. }
  1613. /* If we are on the second or later slice, restore our index. */
  1614. if (start_y != 0)
  1615. new_index = save_state[0];
  1616. for(y=start_y; y<h; y++){
  1617. int x = 0;
  1618. int v;
  1619. DWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
  1620. memset(line, 0, b->width*sizeof(DWTELEM));
  1621. v = b->x_coeff[new_index].coeff;
  1622. x = b->x_coeff[new_index++].x;
  1623. while(x < w)
  1624. {
  1625. if (v < 0)
  1626. line[x] = -(( -v*qmul + qadd)>>(QEXPSHIFT));
  1627. else
  1628. line[x] = (( v*qmul + qadd)>>(QEXPSHIFT));
  1629. v = b->x_coeff[new_index].coeff;
  1630. x = b->x_coeff[new_index++].x;
  1631. }
  1632. }
  1633. if(w > 200 && start_y != 0/*level+1 == s->spatial_decomposition_count*/){
  1634. STOP_TIMER("decode_subband")
  1635. }
  1636. /* Save our variables for the next slice. */
  1637. save_state[0] = new_index;
  1638. return;
  1639. }
  1640. static void reset_contexts(SnowContext *s){
  1641. int plane_index, level, orientation;
  1642. for(plane_index=0; plane_index<3; plane_index++){
  1643. for(level=0; level<s->spatial_decomposition_count; level++){
  1644. for(orientation=level ? 1:0; orientation<4; orientation++){
  1645. memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
  1646. }
  1647. }
  1648. }
  1649. memset(s->header_state, MID_STATE, sizeof(s->header_state));
  1650. memset(s->block_state, MID_STATE, sizeof(s->block_state));
  1651. }
  1652. static int alloc_blocks(SnowContext *s){
  1653. int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
  1654. int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
  1655. s->b_width = w;
  1656. s->b_height= h;
  1657. s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
  1658. return 0;
  1659. }
  1660. static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
  1661. uint8_t *bytestream= d->bytestream;
  1662. uint8_t *bytestream_start= d->bytestream_start;
  1663. *d= *s;
  1664. d->bytestream= bytestream;
  1665. d->bytestream_start= bytestream_start;
  1666. }
  1667. //near copy & paste from dsputil, FIXME
  1668. static int pix_sum(uint8_t * pix, int line_size, int w)
  1669. {
  1670. int s, i, j;
  1671. s = 0;
  1672. for (i = 0; i < w; i++) {
  1673. for (j = 0; j < w; j++) {
  1674. s += pix[0];
  1675. pix ++;
  1676. }
  1677. pix += line_size - w;
  1678. }
  1679. return s;
  1680. }
  1681. //near copy & paste from dsputil, FIXME
  1682. static int pix_norm1(uint8_t * pix, int line_size, int w)
  1683. {
  1684. int s, i, j;
  1685. uint32_t *sq = squareTbl + 256;
  1686. s = 0;
  1687. for (i = 0; i < w; i++) {
  1688. for (j = 0; j < w; j ++) {
  1689. s += sq[pix[0]];
  1690. pix ++;
  1691. }
  1692. pix += line_size - w;
  1693. }
  1694. return s;
  1695. }
  1696. 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){
  1697. const int w= s->b_width << s->block_max_depth;
  1698. const int rem_depth= s->block_max_depth - level;
  1699. const int index= (x + y*w) << rem_depth;
  1700. const int block_w= 1<<rem_depth;
  1701. BlockNode block;
  1702. int i,j;
  1703. block.color[0]= l;
  1704. block.color[1]= cb;
  1705. block.color[2]= cr;
  1706. block.mx= mx;
  1707. block.my= my;
  1708. block.type= type;
  1709. block.level= level;
  1710. for(j=0; j<block_w; j++){
  1711. for(i=0; i<block_w; i++){
  1712. s->block[index + i + j*w]= block;
  1713. }
  1714. }
  1715. }
  1716. 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){
  1717. const int offset[3]= {
  1718. y*c-> stride + x,
  1719. ((y*c->uvstride + x)>>1),
  1720. ((y*c->uvstride + x)>>1),
  1721. };
  1722. int i;
  1723. for(i=0; i<3; i++){
  1724. c->src[0][i]= src [i];
  1725. c->ref[0][i]= ref [i] + offset[i];
  1726. }
  1727. assert(!ref_index);
  1728. }
  1729. //FIXME copy&paste
  1730. #define P_LEFT P[1]
  1731. #define P_TOP P[2]
  1732. #define P_TOPRIGHT P[3]
  1733. #define P_MEDIAN P[4]
  1734. #define P_MV1 P[9]
  1735. #define FLAG_QPEL 1 //must be 1
  1736. static int encode_q_branch(SnowContext *s, int level, int x, int y){
  1737. uint8_t p_buffer[1024];
  1738. uint8_t i_buffer[1024];
  1739. uint8_t p_state[sizeof(s->block_state)];
  1740. uint8_t i_state[sizeof(s->block_state)];
  1741. RangeCoder pc, ic;
  1742. uint8_t *pbbak= s->c.bytestream;
  1743. uint8_t *pbbak_start= s->c.bytestream_start;
  1744. int score, score2, iscore, i_len, p_len, block_s, sum;
  1745. const int w= s->b_width << s->block_max_depth;
  1746. const int h= s->b_height << s->block_max_depth;
  1747. const int rem_depth= s->block_max_depth - level;
  1748. const int index= (x + y*w) << rem_depth;
  1749. const int block_w= 1<<(LOG2_MB_SIZE - level);
  1750. static BlockNode null_block= { //FIXME add border maybe
  1751. .color= {128,128,128},
  1752. .mx= 0,
  1753. .my= 0,
  1754. .type= 0,
  1755. .level= 0,
  1756. };
  1757. int trx= (x+1)<<rem_depth;
  1758. int try= (y+1)<<rem_depth;
  1759. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1760. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1761. BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
  1762. BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
  1763. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1764. BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1765. int pl = left->color[0];
  1766. int pcb= left->color[1];
  1767. int pcr= left->color[2];
  1768. int pmx= mid_pred(left->mx, top->mx, tr->mx);
  1769. int pmy= mid_pred(left->my, top->my, tr->my);
  1770. int mx=0, my=0;
  1771. int l,cr,cb, i;
  1772. const int stride= s->current_picture.linesize[0];
  1773. const int uvstride= s->current_picture.linesize[1];
  1774. const int instride= s->input_picture.linesize[0];
  1775. const int uvinstride= s->input_picture.linesize[1];
  1776. uint8_t *new_l = s->input_picture.data[0] + (x + y* instride)*block_w;
  1777. uint8_t *new_cb= s->input_picture.data[1] + (x + y*uvinstride)*block_w/2;
  1778. uint8_t *new_cr= s->input_picture.data[2] + (x + y*uvinstride)*block_w/2;
  1779. uint8_t current_mb[3][stride*block_w];
  1780. uint8_t *current_data[3]= {&current_mb[0][0], &current_mb[1][0], &current_mb[2][0]};
  1781. int P[10][2];
  1782. int16_t last_mv[3][2];
  1783. int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
  1784. const int shift= 1+qpel;
  1785. MotionEstContext *c= &s->m.me;
  1786. int mx_context= av_log2(2*ABS(left->mx - top->mx));
  1787. int my_context= av_log2(2*ABS(left->my - top->my));
  1788. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1789. assert(sizeof(s->block_state) >= 256);
  1790. if(s->keyframe){
  1791. set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, BLOCK_INTRA);
  1792. return 0;
  1793. }
  1794. //FIXME optimize
  1795. for(i=0; i<block_w; i++)
  1796. memcpy(&current_mb[0][0] + stride*i, new_l + instride*i, block_w);
  1797. for(i=0; i<block_w>>1; i++)
  1798. memcpy(&current_mb[1][0] + uvstride*i, new_cb + uvinstride*i, block_w>>1);
  1799. for(i=0; i<block_w>>1; i++)
  1800. memcpy(&current_mb[2][0] + uvstride*i, new_cr + uvinstride*i, block_w>>1);
  1801. // clip predictors / edge ?
  1802. P_LEFT[0]= left->mx;
  1803. P_LEFT[1]= left->my;
  1804. P_TOP [0]= top->mx;
  1805. P_TOP [1]= top->my;
  1806. P_TOPRIGHT[0]= tr->mx;
  1807. P_TOPRIGHT[1]= tr->my;
  1808. last_mv[0][0]= s->block[index].mx;
  1809. last_mv[0][1]= s->block[index].my;
  1810. last_mv[1][0]= right->mx;
  1811. last_mv[1][1]= right->my;
  1812. last_mv[2][0]= bottom->mx;
  1813. last_mv[2][1]= bottom->my;
  1814. s->m.mb_stride=2;
  1815. s->m.mb_x=
  1816. s->m.mb_y= 0;
  1817. s->m.me.skip= 0;
  1818. init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0);
  1819. assert(s->m.me. stride == stride);
  1820. assert(s->m.me.uvstride == uvstride);
  1821. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1822. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1823. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1824. c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
  1825. c->xmin = - x*block_w - 16+2;
  1826. c->ymin = - y*block_w - 16+2;
  1827. c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
  1828. c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
  1829. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1830. if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
  1831. if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
  1832. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1833. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1834. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
  1835. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1836. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1837. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1838. if (!y) {
  1839. c->pred_x= P_LEFT[0];
  1840. c->pred_y= P_LEFT[1];
  1841. } else {
  1842. c->pred_x = P_MEDIAN[0];
  1843. c->pred_y = P_MEDIAN[1];
  1844. }
  1845. score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, /*ref_index*/ 0, last_mv,
  1846. (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
  1847. assert(mx >= c->xmin);
  1848. assert(mx <= c->xmax);
  1849. assert(my >= c->ymin);
  1850. assert(my <= c->ymax);
  1851. score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
  1852. score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
  1853. //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2
  1854. // subpel search
  1855. pc= s->c;
  1856. pc.bytestream_start=
  1857. pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
  1858. memcpy(p_state, s->block_state, sizeof(s->block_state));
  1859. if(level!=s->block_max_depth)
  1860. put_rac(&pc, &p_state[4 + s_context], 1);
  1861. put_rac(&pc, &p_state[1 + left->type + top->type], 0);
  1862. put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1);
  1863. put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1);
  1864. p_len= pc.bytestream - pc.bytestream_start;
  1865. score += (s->lambda2*(p_len*8
  1866. + (pc.outstanding_count - s->c.outstanding_count)*8
  1867. + (-av_log2(pc.range) + av_log2(s->c.range))
  1868. ))>>FF_LAMBDA_SHIFT;
  1869. block_s= block_w*block_w;
  1870. sum = pix_sum(&current_mb[0][0], stride, block_w);
  1871. l= (sum + block_s/2)/block_s;
  1872. iscore = pix_norm1(&current_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s;
  1873. block_s= block_w*block_w>>2;
  1874. sum = pix_sum(&current_mb[1][0], uvstride, block_w>>1);
  1875. cb= (sum + block_s/2)/block_s;
  1876. // iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
  1877. sum = pix_sum(&current_mb[2][0], uvstride, block_w>>1);
  1878. cr= (sum + block_s/2)/block_s;
  1879. // iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
  1880. ic= s->c;
  1881. ic.bytestream_start=
  1882. ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
  1883. memcpy(i_state, s->block_state, sizeof(s->block_state));
  1884. if(level!=s->block_max_depth)
  1885. put_rac(&ic, &i_state[4 + s_context], 1);
  1886. put_rac(&ic, &i_state[1 + left->type + top->type], 1);
  1887. put_symbol(&ic, &i_state[32], l-pl , 1);
  1888. put_symbol(&ic, &i_state[64], cb-pcb, 1);
  1889. put_symbol(&ic, &i_state[96], cr-pcr, 1);
  1890. i_len= ic.bytestream - ic.bytestream_start;
  1891. iscore += (s->lambda2*(i_len*8
  1892. + (ic.outstanding_count - s->c.outstanding_count)*8
  1893. + (-av_log2(ic.range) + av_log2(s->c.range))
  1894. ))>>FF_LAMBDA_SHIFT;
  1895. // assert(score==256*256*256*64-1);
  1896. assert(iscore < 255*255*256 + s->lambda2*10);
  1897. assert(iscore >= 0);
  1898. assert(l>=0 && l<=255);
  1899. assert(pl>=0 && pl<=255);
  1900. if(level==0){
  1901. int varc= iscore >> 8;
  1902. int vard= score >> 8;
  1903. if (vard <= 64 || vard < varc)
  1904. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1905. else
  1906. c->scene_change_score+= s->m.qscale;
  1907. }
  1908. if(level!=s->block_max_depth){
  1909. put_rac(&s->c, &s->block_state[4 + s_context], 0);
  1910. score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1911. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1912. score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1913. score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1914. score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
  1915. if(score2 < score && score2 < iscore)
  1916. return score2;
  1917. }
  1918. if(iscore < score){
  1919. memcpy(pbbak, i_buffer, i_len);
  1920. s->c= ic;
  1921. s->c.bytestream_start= pbbak_start;
  1922. s->c.bytestream= pbbak + i_len;
  1923. set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA);
  1924. memcpy(s->block_state, i_state, sizeof(s->block_state));
  1925. return iscore;
  1926. }else{
  1927. memcpy(pbbak, p_buffer, p_len);
  1928. s->c= pc;
  1929. s->c.bytestream_start= pbbak_start;
  1930. s->c.bytestream= pbbak + p_len;
  1931. set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0);
  1932. memcpy(s->block_state, p_state, sizeof(s->block_state));
  1933. return score;
  1934. }
  1935. }
  1936. static void decode_q_branch(SnowContext *s, int level, int x, int y){
  1937. const int w= s->b_width << s->block_max_depth;
  1938. const int rem_depth= s->block_max_depth - level;
  1939. const int index= (x + y*w) << rem_depth;
  1940. static BlockNode null_block= { //FIXME add border maybe
  1941. .color= {128,128,128},
  1942. .mx= 0,
  1943. .my= 0,
  1944. .type= 0,
  1945. .level= 0,
  1946. };
  1947. int trx= (x+1)<<rem_depth;
  1948. BlockNode *left = x ? &s->block[index-1] : &null_block;
  1949. BlockNode *top = y ? &s->block[index-w] : &null_block;
  1950. BlockNode *tl = y && x ? &s->block[index-w-1] : left;
  1951. BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
  1952. int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
  1953. if(s->keyframe){
  1954. 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);
  1955. return;
  1956. }
  1957. if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
  1958. int type;
  1959. int l = left->color[0];
  1960. int cb= left->color[1];
  1961. int cr= left->color[2];
  1962. int mx= mid_pred(left->mx, top->mx, tr->mx);
  1963. int my= mid_pred(left->my, top->my, tr->my);
  1964. int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx));
  1965. int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my));
  1966. type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
  1967. if(type){
  1968. l += get_symbol(&s->c, &s->block_state[32], 1);
  1969. cb+= get_symbol(&s->c, &s->block_state[64], 1);
  1970. cr+= get_symbol(&s->c, &s->block_state[96], 1);
  1971. }else{
  1972. mx+= get_symbol(&s->c, &s->block_state[128 + 32*mx_context], 1);
  1973. my+= get_symbol(&s->c, &s->block_state[128 + 32*my_context], 1);
  1974. }
  1975. set_blocks(s, level, x, y, l, cb, cr, mx, my, type);
  1976. }else{
  1977. decode_q_branch(s, level+1, 2*x+0, 2*y+0);
  1978. decode_q_branch(s, level+1, 2*x+1, 2*y+0);
  1979. decode_q_branch(s, level+1, 2*x+0, 2*y+1);
  1980. decode_q_branch(s, level+1, 2*x+1, 2*y+1);
  1981. }
  1982. }
  1983. static void encode_blocks(SnowContext *s){
  1984. int x, y;
  1985. int w= s->b_width;
  1986. int h= s->b_height;
  1987. for(y=0; y<h; y++){
  1988. if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
  1989. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  1990. return;
  1991. }
  1992. for(x=0; x<w; x++){
  1993. encode_q_branch(s, 0, x, y);
  1994. }
  1995. }
  1996. }
  1997. static void decode_blocks(SnowContext *s){
  1998. int x, y;
  1999. int w= s->b_width;
  2000. int h= s->b_height;
  2001. for(y=0; y<h; y++){
  2002. for(x=0; x<w; x++){
  2003. decode_q_branch(s, 0, x, y);
  2004. }
  2005. }
  2006. }
  2007. 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){
  2008. int x, y;
  2009. START_TIMER
  2010. for(y=0; y < b_h+5; y++){
  2011. for(x=0; x < b_w; x++){
  2012. int a0= src[x ];
  2013. int a1= src[x + 1];
  2014. int a2= src[x + 2];
  2015. int a3= src[x + 3];
  2016. int a4= src[x + 4];
  2017. int a5= src[x + 5];
  2018. // int am= 9*(a1+a2) - (a0+a3);
  2019. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  2020. // int am= 18*(a2+a3) - 2*(a1+a4);
  2021. // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  2022. // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
  2023. // if(b_w==16) am= 8*(a1+a2);
  2024. if(dx<8) tmp[x]= (32*a2*( 8-dx) + am* dx + 128)>>8;
  2025. else tmp[x]= ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
  2026. /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
  2027. else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
  2028. else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
  2029. else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
  2030. }
  2031. tmp += stride;
  2032. src += stride;
  2033. }
  2034. tmp -= (b_h+5)*stride;
  2035. for(y=0; y < b_h; y++){
  2036. for(x=0; x < b_w; x++){
  2037. int a0= tmp[x + 0*stride];
  2038. int a1= tmp[x + 1*stride];
  2039. int a2= tmp[x + 2*stride];
  2040. int a3= tmp[x + 3*stride];
  2041. int a4= tmp[x + 4*stride];
  2042. int a5= tmp[x + 5*stride];
  2043. int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
  2044. // int am= 18*(a2+a3) - 2*(a1+a4);
  2045. /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
  2046. int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
  2047. // if(b_w==16) am= 8*(a1+a2);
  2048. if(dy<8) dst[x]= (32*a2*( 8-dy) + am* dy + 128)>>8;
  2049. else dst[x]= ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
  2050. /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
  2051. else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
  2052. else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
  2053. else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
  2054. }
  2055. dst += stride;
  2056. tmp += stride;
  2057. }
  2058. STOP_TIMER("mc_block")
  2059. }
  2060. #define mca(dx,dy,b_w)\
  2061. static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, uint8_t *src, int stride, int h){\
  2062. uint8_t tmp[stride*(b_w+5)];\
  2063. assert(h==b_w);\
  2064. mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
  2065. }
  2066. mca( 0, 0,16)
  2067. mca( 8, 0,16)
  2068. mca( 0, 8,16)
  2069. mca( 8, 8,16)
  2070. mca( 0, 0,8)
  2071. mca( 8, 0,8)
  2072. mca( 0, 8,8)
  2073. mca( 8, 8,8)
  2074. 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){
  2075. if(block->type){
  2076. int x, y;
  2077. const int color= block->color[plane_index];
  2078. for(y=0; y < b_h; y++){
  2079. for(x=0; x < b_w; x++){
  2080. dst[x + y*stride]= color;
  2081. }
  2082. }
  2083. }else{
  2084. const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
  2085. int mx= block->mx*scale;
  2086. int my= block->my*scale;
  2087. const int dx= mx&15;
  2088. const int dy= my&15;
  2089. sx += (mx>>4) - 2;
  2090. sy += (my>>4) - 2;
  2091. src += sx + sy*stride;
  2092. if( (unsigned)sx >= w - b_w - 4
  2093. || (unsigned)sy >= h - b_h - 4){
  2094. ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+5, b_h+5, sx, sy, w, h);
  2095. src= tmp + MB_SIZE;
  2096. }
  2097. if((dx&3) || (dy&3) || b_w!=b_h || (b_w!=4 && b_w!=8 && b_w!=16))
  2098. mc_block(dst, src, tmp, stride, b_w, b_h, dx, dy);
  2099. else
  2100. s->dsp.put_h264_qpel_pixels_tab[2-(b_w>>3)][dy+(dx>>2)](dst,src + 2 + 2*stride,stride);
  2101. }
  2102. }
  2103. static always_inline int same_block(BlockNode *a, BlockNode *b){
  2104. return !((a->mx - b->mx) | (a->my - b->my) | a->type | b->type);
  2105. }
  2106. //FIXME name clenup (b_w, block_w, b_width stuff)
  2107. 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){
  2108. DWTELEM * dst = NULL;
  2109. const int b_width = s->b_width << s->block_max_depth;
  2110. const int b_height= s->b_height << s->block_max_depth;
  2111. const int b_stride= b_width;
  2112. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  2113. BlockNode *rt= lt+1;
  2114. BlockNode *lb= lt+b_stride;
  2115. BlockNode *rb= lb+1;
  2116. uint8_t *block[4];
  2117. uint8_t tmp[src_stride*(b_h+5)]; //FIXME align
  2118. int x,y;
  2119. if(b_x<0){
  2120. lt= rt;
  2121. lb= rb;
  2122. }else if(b_x + 1 >= b_width){
  2123. rt= lt;
  2124. rb= lb;
  2125. }
  2126. if(b_y<0){
  2127. lt= lb;
  2128. rt= rb;
  2129. }else if(b_y + 1 >= b_height){
  2130. lb= lt;
  2131. rb= rt;
  2132. }
  2133. if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
  2134. obmc -= src_x;
  2135. b_w += src_x;
  2136. src_x=0;
  2137. }else if(src_x + b_w > w){
  2138. b_w = w - src_x;
  2139. }
  2140. if(src_y<0){
  2141. obmc -= src_y*obmc_stride;
  2142. b_h += src_y;
  2143. src_y=0;
  2144. }else if(src_y + b_h> h){
  2145. b_h = h - src_y;
  2146. }
  2147. if(b_w<=0 || b_h<=0) return;
  2148. assert(src_stride > 7*MB_SIZE);
  2149. // old_dst += src_x + src_y*dst_stride;
  2150. dst8+= src_x + src_y*src_stride;
  2151. // src += src_x + src_y*src_stride;
  2152. block[0]= tmp+3*MB_SIZE;
  2153. pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  2154. if(same_block(lt, rt)){
  2155. block[1]= block[0];
  2156. }else{
  2157. block[1]= tmp + 4*MB_SIZE;
  2158. pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  2159. }
  2160. if(same_block(lt, lb)){
  2161. block[2]= block[0];
  2162. }else if(same_block(rt, lb)){
  2163. block[2]= block[1];
  2164. }else{
  2165. block[2]= tmp+5*MB_SIZE;
  2166. pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  2167. }
  2168. if(same_block(lt, rb) ){
  2169. block[3]= block[0];
  2170. }else if(same_block(rt, rb)){
  2171. block[3]= block[1];
  2172. }else if(same_block(lb, rb)){
  2173. block[3]= block[2];
  2174. }else{
  2175. block[3]= tmp+6*MB_SIZE;
  2176. pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  2177. }
  2178. #if 0
  2179. for(y=0; y<b_h; y++){
  2180. for(x=0; x<b_w; x++){
  2181. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  2182. if(add) dst[x + y*dst_stride] += v;
  2183. else dst[x + y*dst_stride] -= v;
  2184. }
  2185. }
  2186. for(y=0; y<b_h; y++){
  2187. uint8_t *obmc2= obmc + (obmc_stride>>1);
  2188. for(x=0; x<b_w; x++){
  2189. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  2190. if(add) dst[x + y*dst_stride] += v;
  2191. else dst[x + y*dst_stride] -= v;
  2192. }
  2193. }
  2194. for(y=0; y<b_h; y++){
  2195. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2196. for(x=0; x<b_w; x++){
  2197. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  2198. if(add) dst[x + y*dst_stride] += v;
  2199. else dst[x + y*dst_stride] -= v;
  2200. }
  2201. }
  2202. for(y=0; y<b_h; y++){
  2203. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2204. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2205. for(x=0; x<b_w; x++){
  2206. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  2207. if(add) dst[x + y*dst_stride] += v;
  2208. else dst[x + y*dst_stride] -= v;
  2209. }
  2210. }
  2211. #else
  2212. {
  2213. START_TIMER
  2214. int block_index = 0;
  2215. for(y=0; y<b_h; y++){
  2216. //FIXME ugly missue of obmc_stride
  2217. uint8_t *obmc1= obmc + y*obmc_stride;
  2218. uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2219. uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2220. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2221. dst = slice_buffer_get_line(sb, src_y + y);
  2222. for(x=0; x<b_w; x++){
  2223. int v= obmc1[x] * block[3][x + y*src_stride]
  2224. +obmc2[x] * block[2][x + y*src_stride]
  2225. +obmc3[x] * block[1][x + y*src_stride]
  2226. +obmc4[x] * block[0][x + y*src_stride];
  2227. v <<= 8 - LOG2_OBMC_MAX;
  2228. if(FRAC_BITS != 8){
  2229. v += 1<<(7 - FRAC_BITS);
  2230. v >>= 8 - FRAC_BITS;
  2231. }
  2232. if(add){
  2233. // v += old_dst[x + y*dst_stride];
  2234. v += dst[x + src_x];
  2235. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2236. if(v&(~255)) v= ~(v>>31);
  2237. dst8[x + y*src_stride] = v;
  2238. }else{
  2239. // old_dst[x + y*dst_stride] -= v;
  2240. dst[x + src_x] -= v;
  2241. }
  2242. }
  2243. }
  2244. STOP_TIMER("Inner add y block")
  2245. }
  2246. #endif
  2247. }
  2248. //FIXME name clenup (b_w, block_w, b_width stuff)
  2249. 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){
  2250. const int b_width = s->b_width << s->block_max_depth;
  2251. const int b_height= s->b_height << s->block_max_depth;
  2252. const int b_stride= b_width;
  2253. BlockNode *lt= &s->block[b_x + b_y*b_stride];
  2254. BlockNode *rt= lt+1;
  2255. BlockNode *lb= lt+b_stride;
  2256. BlockNode *rb= lb+1;
  2257. uint8_t *block[4];
  2258. uint8_t tmp[src_stride*(b_h+5)]; //FIXME align
  2259. int x,y;
  2260. if(b_x<0){
  2261. lt= rt;
  2262. lb= rb;
  2263. }else if(b_x + 1 >= b_width){
  2264. rt= lt;
  2265. rb= lb;
  2266. }
  2267. if(b_y<0){
  2268. lt= lb;
  2269. rt= rb;
  2270. }else if(b_y + 1 >= b_height){
  2271. lb= lt;
  2272. rb= rt;
  2273. }
  2274. if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
  2275. obmc -= src_x;
  2276. b_w += src_x;
  2277. src_x=0;
  2278. }else if(src_x + b_w > w){
  2279. b_w = w - src_x;
  2280. }
  2281. if(src_y<0){
  2282. obmc -= src_y*obmc_stride;
  2283. b_h += src_y;
  2284. src_y=0;
  2285. }else if(src_y + b_h> h){
  2286. b_h = h - src_y;
  2287. }
  2288. if(b_w<=0 || b_h<=0) return;
  2289. assert(src_stride > 7*MB_SIZE);
  2290. dst += src_x + src_y*dst_stride;
  2291. dst8+= src_x + src_y*src_stride;
  2292. // src += src_x + src_y*src_stride;
  2293. block[0]= tmp+3*MB_SIZE;
  2294. pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
  2295. if(same_block(lt, rt)){
  2296. block[1]= block[0];
  2297. }else{
  2298. block[1]= tmp + 4*MB_SIZE;
  2299. pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
  2300. }
  2301. if(same_block(lt, lb)){
  2302. block[2]= block[0];
  2303. }else if(same_block(rt, lb)){
  2304. block[2]= block[1];
  2305. }else{
  2306. block[2]= tmp+5*MB_SIZE;
  2307. pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
  2308. }
  2309. if(same_block(lt, rb) ){
  2310. block[3]= block[0];
  2311. }else if(same_block(rt, rb)){
  2312. block[3]= block[1];
  2313. }else if(same_block(lb, rb)){
  2314. block[3]= block[2];
  2315. }else{
  2316. block[3]= tmp+6*MB_SIZE;
  2317. pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
  2318. }
  2319. #if 0
  2320. for(y=0; y<b_h; y++){
  2321. for(x=0; x<b_w; x++){
  2322. int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
  2323. if(add) dst[x + y*dst_stride] += v;
  2324. else dst[x + y*dst_stride] -= v;
  2325. }
  2326. }
  2327. for(y=0; y<b_h; y++){
  2328. uint8_t *obmc2= obmc + (obmc_stride>>1);
  2329. for(x=0; x<b_w; x++){
  2330. int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
  2331. if(add) dst[x + y*dst_stride] += v;
  2332. else dst[x + y*dst_stride] -= v;
  2333. }
  2334. }
  2335. for(y=0; y<b_h; y++){
  2336. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2337. for(x=0; x<b_w; x++){
  2338. int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
  2339. if(add) dst[x + y*dst_stride] += v;
  2340. else dst[x + y*dst_stride] -= v;
  2341. }
  2342. }
  2343. for(y=0; y<b_h; y++){
  2344. uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
  2345. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2346. for(x=0; x<b_w; x++){
  2347. int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
  2348. if(add) dst[x + y*dst_stride] += v;
  2349. else dst[x + y*dst_stride] -= v;
  2350. }
  2351. }
  2352. #else
  2353. for(y=0; y<b_h; y++){
  2354. //FIXME ugly missue of obmc_stride
  2355. uint8_t *obmc1= obmc + y*obmc_stride;
  2356. uint8_t *obmc2= obmc1+ (obmc_stride>>1);
  2357. uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
  2358. uint8_t *obmc4= obmc3+ (obmc_stride>>1);
  2359. for(x=0; x<b_w; x++){
  2360. int v= obmc1[x] * block[3][x + y*src_stride]
  2361. +obmc2[x] * block[2][x + y*src_stride]
  2362. +obmc3[x] * block[1][x + y*src_stride]
  2363. +obmc4[x] * block[0][x + y*src_stride];
  2364. v <<= 8 - LOG2_OBMC_MAX;
  2365. if(FRAC_BITS != 8){
  2366. v += 1<<(7 - FRAC_BITS);
  2367. v >>= 8 - FRAC_BITS;
  2368. }
  2369. if(add){
  2370. v += dst[x + y*dst_stride];
  2371. v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
  2372. if(v&(~255)) v= ~(v>>31);
  2373. dst8[x + y*src_stride] = v;
  2374. }else{
  2375. dst[x + y*dst_stride] -= v;
  2376. }
  2377. }
  2378. }
  2379. #endif
  2380. }
  2381. static always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, DWTELEM * old_buffer, int plane_index, int add, int mb_y){
  2382. Plane *p= &s->plane[plane_index];
  2383. const int mb_w= s->b_width << s->block_max_depth;
  2384. const int mb_h= s->b_height << s->block_max_depth;
  2385. int x, y, mb_x;
  2386. int block_size = MB_SIZE >> s->block_max_depth;
  2387. int block_w = plane_index ? block_size/2 : block_size;
  2388. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2389. int obmc_stride= plane_index ? block_size : 2*block_size;
  2390. int ref_stride= s->current_picture.linesize[plane_index];
  2391. uint8_t *ref = s->last_picture.data[plane_index];
  2392. uint8_t *dst8= s->current_picture.data[plane_index];
  2393. int w= p->width;
  2394. int h= p->height;
  2395. START_TIMER
  2396. if(s->keyframe || (s->avctx->debug&512)){
  2397. if(mb_y==mb_h)
  2398. return;
  2399. if(add){
  2400. for(y=block_w*mb_y; y<block_w*(mb_y+1); y++)
  2401. {
  2402. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2403. DWTELEM * line = sb->line[y];
  2404. for(x=0; x<w; x++)
  2405. {
  2406. // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2407. int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2408. v >>= FRAC_BITS;
  2409. if(v&(~255)) v= ~(v>>31);
  2410. dst8[x + y*ref_stride]= v;
  2411. }
  2412. }
  2413. }else{
  2414. for(y=block_w*mb_y; y<block_w*(mb_y+1); y++)
  2415. {
  2416. // DWTELEM * line = slice_buffer_get_line(sb, y);
  2417. DWTELEM * line = sb->line[y];
  2418. for(x=0; x<w; x++)
  2419. {
  2420. line[x] -= 128 << FRAC_BITS;
  2421. // buf[x + y*w]-= 128<<FRAC_BITS;
  2422. }
  2423. }
  2424. }
  2425. return;
  2426. }
  2427. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2428. START_TIMER
  2429. add_yblock_buffered(s, sb, old_buffer, dst8, ref, obmc,
  2430. block_w*mb_x - block_w/2,
  2431. block_w*mb_y - block_w/2,
  2432. block_w, block_w,
  2433. w, h,
  2434. w, ref_stride, obmc_stride,
  2435. mb_x - 1, mb_y - 1,
  2436. add, plane_index);
  2437. STOP_TIMER("add_yblock")
  2438. }
  2439. STOP_TIMER("predict_slice")
  2440. }
  2441. static always_inline void predict_slice(SnowContext *s, DWTELEM *buf, int plane_index, int add, int mb_y){
  2442. Plane *p= &s->plane[plane_index];
  2443. const int mb_w= s->b_width << s->block_max_depth;
  2444. const int mb_h= s->b_height << s->block_max_depth;
  2445. int x, y, mb_x;
  2446. int block_size = MB_SIZE >> s->block_max_depth;
  2447. int block_w = plane_index ? block_size/2 : block_size;
  2448. const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
  2449. int obmc_stride= plane_index ? block_size : 2*block_size;
  2450. int ref_stride= s->current_picture.linesize[plane_index];
  2451. uint8_t *ref = s->last_picture.data[plane_index];
  2452. uint8_t *dst8= s->current_picture.data[plane_index];
  2453. int w= p->width;
  2454. int h= p->height;
  2455. START_TIMER
  2456. if(s->keyframe || (s->avctx->debug&512)){
  2457. if(mb_y==mb_h)
  2458. return;
  2459. if(add){
  2460. for(y=block_w*mb_y; y<block_w*(mb_y+1); y++){
  2461. for(x=0; x<w; x++){
  2462. int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
  2463. v >>= FRAC_BITS;
  2464. if(v&(~255)) v= ~(v>>31);
  2465. dst8[x + y*ref_stride]= v;
  2466. }
  2467. }
  2468. }else{
  2469. for(y=block_w*mb_y; y<block_w*(mb_y+1); y++){
  2470. for(x=0; x<w; x++){
  2471. buf[x + y*w]-= 128<<FRAC_BITS;
  2472. }
  2473. }
  2474. }
  2475. return;
  2476. }
  2477. for(mb_x=0; mb_x<=mb_w; mb_x++){
  2478. START_TIMER
  2479. add_yblock(s, buf, dst8, ref, obmc,
  2480. block_w*mb_x - block_w/2,
  2481. block_w*mb_y - block_w/2,
  2482. block_w, block_w,
  2483. w, h,
  2484. w, ref_stride, obmc_stride,
  2485. mb_x - 1, mb_y - 1,
  2486. add, plane_index);
  2487. STOP_TIMER("add_yblock")
  2488. }
  2489. STOP_TIMER("predict_slice")
  2490. }
  2491. static always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
  2492. const int mb_h= s->b_height << s->block_max_depth;
  2493. int mb_y;
  2494. for(mb_y=0; mb_y<=mb_h; mb_y++)
  2495. predict_slice(s, buf, plane_index, add, mb_y);
  2496. }
  2497. static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
  2498. const int level= b->level;
  2499. const int w= b->width;
  2500. const int h= b->height;
  2501. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  2502. const int qmul= qexp[qlog&7]<<(qlog>>3);
  2503. int x,y, thres1, thres2;
  2504. START_TIMER
  2505. assert(QROOT==8);
  2506. if(s->qlog == LOSSLESS_QLOG) return;
  2507. bias= bias ? 0 : (3*qmul)>>3;
  2508. thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
  2509. thres2= 2*thres1;
  2510. if(!bias){
  2511. for(y=0; y<h; y++){
  2512. for(x=0; x<w; x++){
  2513. int i= src[x + y*stride];
  2514. if((unsigned)(i+thres1) > thres2){
  2515. if(i>=0){
  2516. i<<= QEXPSHIFT;
  2517. i/= qmul; //FIXME optimize
  2518. src[x + y*stride]= i;
  2519. }else{
  2520. i= -i;
  2521. i<<= QEXPSHIFT;
  2522. i/= qmul; //FIXME optimize
  2523. src[x + y*stride]= -i;
  2524. }
  2525. }else
  2526. src[x + y*stride]= 0;
  2527. }
  2528. }
  2529. }else{
  2530. for(y=0; y<h; y++){
  2531. for(x=0; x<w; x++){
  2532. int i= src[x + y*stride];
  2533. if((unsigned)(i+thres1) > thres2){
  2534. if(i>=0){
  2535. i<<= QEXPSHIFT;
  2536. i= (i + bias) / qmul; //FIXME optimize
  2537. src[x + y*stride]= i;
  2538. }else{
  2539. i= -i;
  2540. i<<= QEXPSHIFT;
  2541. i= (i + bias) / qmul; //FIXME optimize
  2542. src[x + y*stride]= -i;
  2543. }
  2544. }else
  2545. src[x + y*stride]= 0;
  2546. }
  2547. }
  2548. }
  2549. if(level+1 == s->spatial_decomposition_count){
  2550. // STOP_TIMER("quantize")
  2551. }
  2552. }
  2553. static void dequantize_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride){
  2554. const int w= b->width;
  2555. const int h= b->height;
  2556. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  2557. const int qmul= qexp[qlog&7]<<(qlog>>3);
  2558. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2559. int x,y;
  2560. START_TIMER
  2561. if(s->qlog == LOSSLESS_QLOG) return;
  2562. assert(QROOT==8);
  2563. for(y=0; y<h; y++){
  2564. // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  2565. DWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  2566. for(x=0; x<w; x++){
  2567. int i= line[x];
  2568. if(i<0){
  2569. line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2570. }else if(i>0){
  2571. line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2572. }
  2573. }
  2574. }
  2575. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  2576. STOP_TIMER("dquant")
  2577. }
  2578. }
  2579. static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
  2580. const int w= b->width;
  2581. const int h= b->height;
  2582. const int qlog= clip(s->qlog + b->qlog, 0, 128);
  2583. const int qmul= qexp[qlog&7]<<(qlog>>3);
  2584. const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
  2585. int x,y;
  2586. START_TIMER
  2587. if(s->qlog == LOSSLESS_QLOG) return;
  2588. assert(QROOT==8);
  2589. for(y=0; y<h; y++){
  2590. for(x=0; x<w; x++){
  2591. int i= src[x + y*stride];
  2592. if(i<0){
  2593. src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
  2594. }else if(i>0){
  2595. src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
  2596. }
  2597. }
  2598. }
  2599. if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
  2600. STOP_TIMER("dquant")
  2601. }
  2602. }
  2603. static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  2604. const int w= b->width;
  2605. const int h= b->height;
  2606. int x,y;
  2607. for(y=h-1; y>=0; y--){
  2608. for(x=w-1; x>=0; x--){
  2609. int i= x + y*stride;
  2610. if(x){
  2611. if(use_median){
  2612. if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2613. else src[i] -= src[i - 1];
  2614. }else{
  2615. if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2616. else src[i] -= src[i - 1];
  2617. }
  2618. }else{
  2619. if(y) src[i] -= src[i - stride];
  2620. }
  2621. }
  2622. }
  2623. }
  2624. static void correlate_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  2625. const int w= b->width;
  2626. const int h= b->height;
  2627. int x,y;
  2628. // START_TIMER
  2629. DWTELEM * line;
  2630. DWTELEM * prev;
  2631. for(y=0; y<h; y++){
  2632. prev = line;
  2633. // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
  2634. line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
  2635. for(x=0; x<w; x++){
  2636. if(x){
  2637. if(use_median){
  2638. if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
  2639. else line[x] += line[x - 1];
  2640. }else{
  2641. if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
  2642. else line[x] += line[x - 1];
  2643. }
  2644. }else{
  2645. if(y) line[x] += prev[x];
  2646. }
  2647. }
  2648. }
  2649. // STOP_TIMER("correlate")
  2650. }
  2651. static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
  2652. const int w= b->width;
  2653. const int h= b->height;
  2654. int x,y;
  2655. for(y=0; y<h; y++){
  2656. for(x=0; x<w; x++){
  2657. int i= x + y*stride;
  2658. if(x){
  2659. if(use_median){
  2660. if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
  2661. else src[i] += src[i - 1];
  2662. }else{
  2663. if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
  2664. else src[i] += src[i - 1];
  2665. }
  2666. }else{
  2667. if(y) src[i] += src[i - stride];
  2668. }
  2669. }
  2670. }
  2671. }
  2672. static void encode_header(SnowContext *s){
  2673. int plane_index, level, orientation;
  2674. uint8_t kstate[32];
  2675. memset(kstate, MID_STATE, sizeof(kstate));
  2676. put_rac(&s->c, kstate, s->keyframe);
  2677. if(s->keyframe || s->always_reset)
  2678. reset_contexts(s);
  2679. if(s->keyframe){
  2680. put_symbol(&s->c, s->header_state, s->version, 0);
  2681. put_rac(&s->c, s->header_state, s->always_reset);
  2682. put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
  2683. put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
  2684. put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
  2685. put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
  2686. put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
  2687. put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
  2688. put_rac(&s->c, s->header_state, s->spatial_scalability);
  2689. // put_rac(&s->c, s->header_state, s->rate_scalability);
  2690. for(plane_index=0; plane_index<2; plane_index++){
  2691. for(level=0; level<s->spatial_decomposition_count; level++){
  2692. for(orientation=level ? 1:0; orientation<4; orientation++){
  2693. if(orientation==2) continue;
  2694. put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
  2695. }
  2696. }
  2697. }
  2698. }
  2699. put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
  2700. put_symbol(&s->c, s->header_state, s->qlog, 1);
  2701. put_symbol(&s->c, s->header_state, s->mv_scale, 0);
  2702. put_symbol(&s->c, s->header_state, s->qbias, 1);
  2703. put_symbol(&s->c, s->header_state, s->block_max_depth, 0);
  2704. }
  2705. static int decode_header(SnowContext *s){
  2706. int plane_index, level, orientation;
  2707. uint8_t kstate[32];
  2708. memset(kstate, MID_STATE, sizeof(kstate));
  2709. s->keyframe= get_rac(&s->c, kstate);
  2710. if(s->keyframe || s->always_reset)
  2711. reset_contexts(s);
  2712. if(s->keyframe){
  2713. s->version= get_symbol(&s->c, s->header_state, 0);
  2714. if(s->version>0){
  2715. av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
  2716. return -1;
  2717. }
  2718. s->always_reset= get_rac(&s->c, s->header_state);
  2719. s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2720. s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2721. s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
  2722. s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
  2723. s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
  2724. s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
  2725. s->spatial_scalability= get_rac(&s->c, s->header_state);
  2726. // s->rate_scalability= get_rac(&s->c, s->header_state);
  2727. for(plane_index=0; plane_index<3; plane_index++){
  2728. for(level=0; level<s->spatial_decomposition_count; level++){
  2729. for(orientation=level ? 1:0; orientation<4; orientation++){
  2730. int q;
  2731. if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
  2732. else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
  2733. else q= get_symbol(&s->c, s->header_state, 1);
  2734. s->plane[plane_index].band[level][orientation].qlog= q;
  2735. }
  2736. }
  2737. }
  2738. }
  2739. s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
  2740. if(s->spatial_decomposition_type > 2){
  2741. av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
  2742. return -1;
  2743. }
  2744. s->qlog= get_symbol(&s->c, s->header_state, 1);
  2745. s->mv_scale= get_symbol(&s->c, s->header_state, 0);
  2746. s->qbias= get_symbol(&s->c, s->header_state, 1);
  2747. s->block_max_depth= get_symbol(&s->c, s->header_state, 0);
  2748. return 0;
  2749. }
  2750. static int common_init(AVCodecContext *avctx){
  2751. SnowContext *s = avctx->priv_data;
  2752. int width, height;
  2753. int level, orientation, plane_index, dec;
  2754. s->avctx= avctx;
  2755. dsputil_init(&s->dsp, avctx);
  2756. #define mcf(dx,dy)\
  2757. s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
  2758. s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
  2759. s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
  2760. s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
  2761. s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
  2762. s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
  2763. mcf( 0, 0)
  2764. mcf( 4, 0)
  2765. mcf( 8, 0)
  2766. mcf(12, 0)
  2767. mcf( 0, 4)
  2768. mcf( 4, 4)
  2769. mcf( 8, 4)
  2770. mcf(12, 4)
  2771. mcf( 0, 8)
  2772. mcf( 4, 8)
  2773. mcf( 8, 8)
  2774. mcf(12, 8)
  2775. mcf( 0,12)
  2776. mcf( 4,12)
  2777. mcf( 8,12)
  2778. mcf(12,12)
  2779. #define mcfh(dx,dy)\
  2780. s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
  2781. s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
  2782. mc_block_hpel ## dx ## dy ## 16;\
  2783. s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
  2784. s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
  2785. mc_block_hpel ## dx ## dy ## 8;
  2786. mcfh(0, 0)
  2787. mcfh(8, 0)
  2788. mcfh(0, 8)
  2789. mcfh(8, 8)
  2790. dec= s->spatial_decomposition_count= 5;
  2791. s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
  2792. s->chroma_h_shift= 1; //FIXME XXX
  2793. s->chroma_v_shift= 1;
  2794. // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
  2795. width= s->avctx->width;
  2796. height= s->avctx->height;
  2797. s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
  2798. s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
  2799. s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
  2800. for(plane_index=0; plane_index<3; plane_index++){
  2801. int w= s->avctx->width;
  2802. int h= s->avctx->height;
  2803. if(plane_index){
  2804. w>>= s->chroma_h_shift;
  2805. h>>= s->chroma_v_shift;
  2806. }
  2807. s->plane[plane_index].width = w;
  2808. s->plane[plane_index].height= h;
  2809. //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
  2810. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  2811. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2812. SubBand *b= &s->plane[plane_index].band[level][orientation];
  2813. b->buf= s->spatial_dwt_buffer;
  2814. b->level= level;
  2815. b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
  2816. b->width = (w + !(orientation&1))>>1;
  2817. b->height= (h + !(orientation>1))>>1;
  2818. b->stride_line = 1 << (s->spatial_decomposition_count - level);
  2819. b->buf_x_offset = 0;
  2820. b->buf_y_offset = 0;
  2821. if(orientation&1){
  2822. b->buf += (w+1)>>1;
  2823. b->buf_x_offset = (w+1)>>1;
  2824. }
  2825. if(orientation>1){
  2826. b->buf += b->stride>>1;
  2827. b->buf_y_offset = b->stride_line >> 1;
  2828. }
  2829. if(level)
  2830. b->parent= &s->plane[plane_index].band[level-1][orientation];
  2831. b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
  2832. }
  2833. w= (w+1)>>1;
  2834. h= (h+1)>>1;
  2835. }
  2836. }
  2837. reset_contexts(s);
  2838. /*
  2839. width= s->width= avctx->width;
  2840. height= s->height= avctx->height;
  2841. assert(width && height);
  2842. */
  2843. s->avctx->get_buffer(s->avctx, &s->mconly_picture);
  2844. return 0;
  2845. }
  2846. static void calculate_vissual_weight(SnowContext *s, Plane *p){
  2847. int width = p->width;
  2848. int height= p->height;
  2849. int level, orientation, x, y;
  2850. for(level=0; level<s->spatial_decomposition_count; level++){
  2851. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  2852. SubBand *b= &p->band[level][orientation];
  2853. DWTELEM *buf= b->buf;
  2854. int64_t error=0;
  2855. memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
  2856. buf[b->width/2 + b->height/2*b->stride]= 256*256;
  2857. ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  2858. for(y=0; y<height; y++){
  2859. for(x=0; x<width; x++){
  2860. int64_t d= s->spatial_dwt_buffer[x + y*width];
  2861. error += d*d;
  2862. }
  2863. }
  2864. b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
  2865. // av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
  2866. }
  2867. }
  2868. }
  2869. static int encode_init(AVCodecContext *avctx)
  2870. {
  2871. SnowContext *s = avctx->priv_data;
  2872. int plane_index;
  2873. if(avctx->strict_std_compliance >= 0){
  2874. av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
  2875. "use vstrict=-1 / -strict -1 to use it anyway\n");
  2876. return -1;
  2877. }
  2878. common_init(avctx);
  2879. alloc_blocks(s);
  2880. s->version=0;
  2881. s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
  2882. s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2883. s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
  2884. h263_encode_init(&s->m); //mv_penalty
  2885. for(plane_index=0; plane_index<3; plane_index++){
  2886. calculate_vissual_weight(s, &s->plane[plane_index]);
  2887. }
  2888. avctx->coded_frame= &s->current_picture;
  2889. switch(avctx->pix_fmt){
  2890. // case PIX_FMT_YUV444P:
  2891. // case PIX_FMT_YUV422P:
  2892. case PIX_FMT_YUV420P:
  2893. case PIX_FMT_GRAY8:
  2894. // case PIX_FMT_YUV411P:
  2895. // case PIX_FMT_YUV410P:
  2896. s->colorspace_type= 0;
  2897. break;
  2898. /* case PIX_FMT_RGBA32:
  2899. s->colorspace= 1;
  2900. break;*/
  2901. default:
  2902. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  2903. return -1;
  2904. }
  2905. // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  2906. s->chroma_h_shift= 1;
  2907. s->chroma_v_shift= 1;
  2908. return 0;
  2909. }
  2910. static int frame_start(SnowContext *s){
  2911. AVFrame tmp;
  2912. int w= s->avctx->width; //FIXME round up to x16 ?
  2913. int h= s->avctx->height;
  2914. if(s->current_picture.data[0]){
  2915. draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
  2916. draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
  2917. draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
  2918. }
  2919. tmp= s->last_picture;
  2920. s->last_picture= s->current_picture;
  2921. s->current_picture= tmp;
  2922. s->current_picture.reference= 1;
  2923. if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
  2924. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  2925. return -1;
  2926. }
  2927. return 0;
  2928. }
  2929. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  2930. SnowContext *s = avctx->priv_data;
  2931. RangeCoder * const c= &s->c;
  2932. AVFrame *pict = data;
  2933. const int width= s->avctx->width;
  2934. const int height= s->avctx->height;
  2935. int level, orientation, plane_index;
  2936. ff_init_range_encoder(c, buf, buf_size);
  2937. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  2938. s->input_picture = *pict;
  2939. s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
  2940. pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  2941. if(pict->quality){
  2942. s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
  2943. //<64 >60
  2944. s->qlog += 61;
  2945. }else{
  2946. s->qlog= LOSSLESS_QLOG;
  2947. }
  2948. frame_start(s);
  2949. s->current_picture.key_frame= s->keyframe;
  2950. if(pict->pict_type == P_TYPE){
  2951. int block_width = (width +15)>>4;
  2952. int block_height= (height+15)>>4;
  2953. int stride= s->current_picture.linesize[0];
  2954. assert(s->current_picture.data[0]);
  2955. assert(s->last_picture.data[0]);
  2956. s->m.avctx= s->avctx;
  2957. s->m.current_picture.data[0]= s->current_picture.data[0];
  2958. s->m. last_picture.data[0]= s-> last_picture.data[0];
  2959. s->m. new_picture.data[0]= s-> input_picture.data[0];
  2960. s->m.current_picture_ptr= &s->m.current_picture;
  2961. s->m. last_picture_ptr= &s->m. last_picture;
  2962. s->m.linesize=
  2963. s->m. last_picture.linesize[0]=
  2964. s->m. new_picture.linesize[0]=
  2965. s->m.current_picture.linesize[0]= stride;
  2966. s->m.uvlinesize= s->current_picture.linesize[1];
  2967. s->m.width = width;
  2968. s->m.height= height;
  2969. s->m.mb_width = block_width;
  2970. s->m.mb_height= block_height;
  2971. s->m.mb_stride= s->m.mb_width+1;
  2972. s->m.b8_stride= 2*s->m.mb_width+1;
  2973. s->m.f_code=1;
  2974. s->m.pict_type= pict->pict_type;
  2975. s->m.me_method= s->avctx->me_method;
  2976. s->m.me.scene_change_score=0;
  2977. s->m.flags= s->avctx->flags;
  2978. s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
  2979. s->m.out_format= FMT_H263;
  2980. s->m.unrestricted_mv= 1;
  2981. s->lambda = s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else
  2982. s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  2983. s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  2984. s->m.dsp= s->dsp; //move
  2985. ff_init_me(&s->m);
  2986. }
  2987. redo_frame:
  2988. s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
  2989. encode_header(s);
  2990. encode_blocks(s);
  2991. for(plane_index=0; plane_index<3; plane_index++){
  2992. Plane *p= &s->plane[plane_index];
  2993. int w= p->width;
  2994. int h= p->height;
  2995. int x, y;
  2996. // int bits= put_bits_count(&s->c.pb);
  2997. //FIXME optimize
  2998. if(pict->data[plane_index]) //FIXME gray hack
  2999. for(y=0; y<h; y++){
  3000. for(x=0; x<w; x++){
  3001. s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
  3002. }
  3003. }
  3004. predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
  3005. if( plane_index==0
  3006. && pict->pict_type == P_TYPE
  3007. && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
  3008. ff_init_range_encoder(c, buf, buf_size);
  3009. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3010. pict->pict_type= FF_I_TYPE;
  3011. s->keyframe=1;
  3012. reset_contexts(s);
  3013. goto redo_frame;
  3014. }
  3015. if(s->qlog == LOSSLESS_QLOG){
  3016. for(y=0; y<h; y++){
  3017. for(x=0; x<w; x++){
  3018. s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1)))>>FRAC_BITS;
  3019. }
  3020. }
  3021. }
  3022. ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3023. for(level=0; level<s->spatial_decomposition_count; level++){
  3024. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3025. SubBand *b= &p->band[level][orientation];
  3026. quantize(s, b, b->buf, b->stride, s->qbias);
  3027. if(orientation==0)
  3028. decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
  3029. encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
  3030. assert(b->parent==NULL || b->parent->stride == b->stride*2);
  3031. if(orientation==0)
  3032. correlate(s, b, b->buf, b->stride, 1, 0);
  3033. }
  3034. }
  3035. // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
  3036. for(level=0; level<s->spatial_decomposition_count; level++){
  3037. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3038. SubBand *b= &p->band[level][orientation];
  3039. dequantize(s, b, b->buf, b->stride);
  3040. }
  3041. }
  3042. ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3043. if(s->qlog == LOSSLESS_QLOG){
  3044. for(y=0; y<h; y++){
  3045. for(x=0; x<w; x++){
  3046. s->spatial_dwt_buffer[y*w + x]<<=FRAC_BITS;
  3047. }
  3048. }
  3049. }
  3050. {START_TIMER
  3051. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3052. STOP_TIMER("pred-conv")}
  3053. if(s->avctx->flags&CODEC_FLAG_PSNR){
  3054. int64_t error= 0;
  3055. if(pict->data[plane_index]) //FIXME gray hack
  3056. for(y=0; y<h; y++){
  3057. for(x=0; x<w; x++){
  3058. 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];
  3059. error += d*d;
  3060. }
  3061. }
  3062. s->avctx->error[plane_index] += error;
  3063. s->current_picture.error[plane_index] = error;
  3064. }
  3065. }
  3066. if(s->last_picture.data[0])
  3067. avctx->release_buffer(avctx, &s->last_picture);
  3068. emms_c();
  3069. return ff_rac_terminate(c);
  3070. }
  3071. static void common_end(SnowContext *s){
  3072. int plane_index, level, orientation;
  3073. av_freep(&s->spatial_dwt_buffer);
  3074. av_freep(&s->m.me.scratchpad);
  3075. av_freep(&s->m.me.map);
  3076. av_freep(&s->m.me.score_map);
  3077. av_freep(&s->block);
  3078. for(plane_index=0; plane_index<3; plane_index++){
  3079. for(level=s->spatial_decomposition_count-1; level>=0; level--){
  3080. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3081. SubBand *b= &s->plane[plane_index].band[level][orientation];
  3082. av_freep(&b->x_coeff);
  3083. }
  3084. }
  3085. }
  3086. }
  3087. static int encode_end(AVCodecContext *avctx)
  3088. {
  3089. SnowContext *s = avctx->priv_data;
  3090. common_end(s);
  3091. return 0;
  3092. }
  3093. static int decode_init(AVCodecContext *avctx)
  3094. {
  3095. SnowContext *s = avctx->priv_data;
  3096. int block_size;
  3097. common_init(avctx);
  3098. block_size = MB_SIZE >> s->block_max_depth;
  3099. /* 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. */
  3100. /* FIXME The formula is WRONG. For height > 480, the buffer will overflow. */
  3101. /* 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. */
  3102. // 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);
  3103. slice_buffer_init(&s->sb, s->plane[0].height, s->plane[0].height, s->plane[0].width, s->spatial_dwt_buffer);
  3104. return 0;
  3105. }
  3106. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
  3107. SnowContext *s = avctx->priv_data;
  3108. RangeCoder * const c= &s->c;
  3109. int bytes_read;
  3110. AVFrame *picture = data;
  3111. int level, orientation, plane_index;
  3112. ff_init_range_decoder(c, buf, buf_size);
  3113. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  3114. s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
  3115. decode_header(s);
  3116. if(!s->block) alloc_blocks(s);
  3117. frame_start(s);
  3118. //keyframe flag dupliaction mess FIXME
  3119. if(avctx->debug&FF_DEBUG_PICT_INFO)
  3120. av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
  3121. decode_blocks(s);
  3122. for(plane_index=0; plane_index<3; plane_index++){
  3123. Plane *p= &s->plane[plane_index];
  3124. int w= p->width;
  3125. int h= p->height;
  3126. int x, y;
  3127. int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
  3128. SubBand * correlate_band;
  3129. if(s->avctx->debug&2048){
  3130. memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
  3131. predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
  3132. for(y=0; y<h; y++){
  3133. for(x=0; x<w; x++){
  3134. int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
  3135. s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
  3136. }
  3137. }
  3138. }
  3139. { START_TIMER
  3140. for(level=0; level<s->spatial_decomposition_count; level++){
  3141. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3142. SubBand *b= &p->band[level][orientation];
  3143. unpack_coeffs(s, b, b->parent, orientation);
  3144. }
  3145. }
  3146. STOP_TIMER("unpack coeffs");
  3147. }
  3148. /* Handle level 0, orientation 0 specially. It is particularly resistant to slicing but fortunately quite small, so process it in one pass. */
  3149. correlate_band = &p->band[0][0];
  3150. decode_subband_slice_buffered(s, correlate_band, &s->sb, 0, correlate_band->height, decode_state[0][0]);
  3151. correlate_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, 1, 0);
  3152. dequantize_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride);
  3153. {START_TIMER
  3154. const int mb_h= s->b_height << s->block_max_depth;
  3155. const int block_size = MB_SIZE >> s->block_max_depth;
  3156. const int block_w = plane_index ? block_size/2 : block_size;
  3157. int mb_y;
  3158. dwt_compose_t cs[MAX_DECOMPOSITIONS];
  3159. int yd=0, yq=0;
  3160. int y;
  3161. int end_y;
  3162. ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3163. for(mb_y=0; mb_y<=mb_h; mb_y++){
  3164. const int slice_starty = block_w*mb_y;
  3165. const int slice_h = block_w*(mb_y+1);
  3166. {
  3167. START_TIMER
  3168. for(level=0; level<s->spatial_decomposition_count; level++){
  3169. for(orientation=level ? 1 : 1; orientation<4; orientation++){
  3170. SubBand *b= &p->band[level][orientation];
  3171. int start_y;
  3172. int end_y;
  3173. int our_mb_start = mb_y;
  3174. int our_mb_end = (mb_y + 1);
  3175. start_y = FFMIN(b->height, (mb_y ? ((block_w * our_mb_start - 4) >> (s->spatial_decomposition_count - level)) + 5 : 0));
  3176. end_y = FFMIN(b->height, (((block_w * our_mb_end - 4) >> (s->spatial_decomposition_count - level)) + 5));
  3177. if (start_y != end_y)
  3178. decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
  3179. }
  3180. }
  3181. STOP_TIMER("decode_subband_slice");
  3182. }
  3183. { START_TIMER
  3184. for(; yd<slice_h; yd+=4){
  3185. ff_spatial_idwt_buffered_slice(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
  3186. }
  3187. STOP_TIMER("idwt slice");}
  3188. if(s->qlog == LOSSLESS_QLOG){
  3189. for(; yq<slice_h && yq<h; yq++){
  3190. DWTELEM * line = slice_buffer_get_line(&s->sb, yq);
  3191. for(x=0; x<w; x++){
  3192. line[x] <<= FRAC_BITS;
  3193. }
  3194. }
  3195. }
  3196. predict_slice_buffered(s, &s->sb, s->spatial_dwt_buffer, plane_index, 1, mb_y);
  3197. /* Nasty hack based empirically on how predict_slice_buffered() hits the buffer. */
  3198. /* 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. */
  3199. if (s->keyframe || (s->avctx->debug&512)){
  3200. y = FFMIN(p->height, slice_starty);
  3201. end_y = FFMIN(p->height, slice_h);
  3202. }
  3203. else{
  3204. y = FFMAX(0, FFMIN(p->height, slice_starty - (block_w >> 1)));
  3205. end_y = FFMAX(0, FFMIN(p->height, slice_h - (block_w >> 1)));
  3206. }
  3207. while(y < end_y)
  3208. slice_buffer_release(&s->sb, y++);
  3209. }
  3210. slice_buffer_flush(&s->sb);
  3211. STOP_TIMER("idwt + predict_slices")}
  3212. }
  3213. emms_c();
  3214. if(s->last_picture.data[0])
  3215. avctx->release_buffer(avctx, &s->last_picture);
  3216. if(!(s->avctx->debug&2048))
  3217. *picture= s->current_picture;
  3218. else
  3219. *picture= s->mconly_picture;
  3220. *data_size = sizeof(AVFrame);
  3221. bytes_read= c->bytestream - c->bytestream_start;
  3222. if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
  3223. return bytes_read;
  3224. }
  3225. static int decode_end(AVCodecContext *avctx)
  3226. {
  3227. SnowContext *s = avctx->priv_data;
  3228. slice_buffer_destroy(&s->sb);
  3229. common_end(s);
  3230. return 0;
  3231. }
  3232. AVCodec snow_decoder = {
  3233. "snow",
  3234. CODEC_TYPE_VIDEO,
  3235. CODEC_ID_SNOW,
  3236. sizeof(SnowContext),
  3237. decode_init,
  3238. NULL,
  3239. decode_end,
  3240. decode_frame,
  3241. 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  3242. NULL
  3243. };
  3244. #ifdef CONFIG_ENCODERS
  3245. AVCodec snow_encoder = {
  3246. "snow",
  3247. CODEC_TYPE_VIDEO,
  3248. CODEC_ID_SNOW,
  3249. sizeof(SnowContext),
  3250. encode_init,
  3251. encode_frame,
  3252. encode_end,
  3253. };
  3254. #endif
  3255. #if 0
  3256. #undef malloc
  3257. #undef free
  3258. #undef printf
  3259. int main(){
  3260. int width=256;
  3261. int height=256;
  3262. int buffer[2][width*height];
  3263. SnowContext s;
  3264. int i;
  3265. s.spatial_decomposition_count=6;
  3266. s.spatial_decomposition_type=1;
  3267. printf("testing 5/3 DWT\n");
  3268. for(i=0; i<width*height; i++)
  3269. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  3270. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3271. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3272. for(i=0; i<width*height; i++)
  3273. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  3274. printf("testing 9/7 DWT\n");
  3275. s.spatial_decomposition_type=0;
  3276. for(i=0; i<width*height; i++)
  3277. buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
  3278. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3279. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3280. for(i=0; i<width*height; i++)
  3281. if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
  3282. printf("testing AC coder\n");
  3283. memset(s.header_state, 0, sizeof(s.header_state));
  3284. ff_init_range_encoder(&s.c, buffer[0], 256*256);
  3285. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3286. for(i=-256; i<256; i++){
  3287. START_TIMER
  3288. put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
  3289. STOP_TIMER("put_symbol")
  3290. }
  3291. ff_rac_terminate(&s.c);
  3292. memset(s.header_state, 0, sizeof(s.header_state));
  3293. ff_init_range_decoder(&s.c, buffer[0], 256*256);
  3294. ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  3295. for(i=-256; i<256; i++){
  3296. int j;
  3297. START_TIMER
  3298. j= get_symbol(&s.c, s.header_state, 1);
  3299. STOP_TIMER("get_symbol")
  3300. if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
  3301. }
  3302. {
  3303. int level, orientation, x, y;
  3304. int64_t errors[8][4];
  3305. int64_t g=0;
  3306. memset(errors, 0, sizeof(errors));
  3307. s.spatial_decomposition_count=3;
  3308. s.spatial_decomposition_type=0;
  3309. for(level=0; level<s.spatial_decomposition_count; level++){
  3310. for(orientation=level ? 1 : 0; orientation<4; orientation++){
  3311. int w= width >> (s.spatial_decomposition_count-level);
  3312. int h= height >> (s.spatial_decomposition_count-level);
  3313. int stride= width << (s.spatial_decomposition_count-level);
  3314. DWTELEM *buf= buffer[0];
  3315. int64_t error=0;
  3316. if(orientation&1) buf+=w;
  3317. if(orientation>1) buf+=stride>>1;
  3318. memset(buffer[0], 0, sizeof(int)*width*height);
  3319. buf[w/2 + h/2*stride]= 256*256;
  3320. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3321. for(y=0; y<height; y++){
  3322. for(x=0; x<width; x++){
  3323. int64_t d= buffer[0][x + y*width];
  3324. error += d*d;
  3325. if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
  3326. }
  3327. if(ABS(height/2-y)<9 && level==2) printf("\n");
  3328. }
  3329. error= (int)(sqrt(error)+0.5);
  3330. errors[level][orientation]= error;
  3331. if(g) g=ff_gcd(g, error);
  3332. else g= error;
  3333. }
  3334. }
  3335. printf("static int const visual_weight[][4]={\n");
  3336. for(level=0; level<s.spatial_decomposition_count; level++){
  3337. printf(" {");
  3338. for(orientation=0; orientation<4; orientation++){
  3339. printf("%8lld,", errors[level][orientation]/g);
  3340. }
  3341. printf("},\n");
  3342. }
  3343. printf("};\n");
  3344. {
  3345. int level=2;
  3346. int orientation=3;
  3347. int w= width >> (s.spatial_decomposition_count-level);
  3348. int h= height >> (s.spatial_decomposition_count-level);
  3349. int stride= width << (s.spatial_decomposition_count-level);
  3350. DWTELEM *buf= buffer[0];
  3351. int64_t error=0;
  3352. buf+=w;
  3353. buf+=stride>>1;
  3354. memset(buffer[0], 0, sizeof(int)*width*height);
  3355. #if 1
  3356. for(y=0; y<height; y++){
  3357. for(x=0; x<width; x++){
  3358. int tab[4]={0,2,3,1};
  3359. buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
  3360. }
  3361. }
  3362. ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3363. #else
  3364. for(y=0; y<h; y++){
  3365. for(x=0; x<w; x++){
  3366. buf[x + y*stride ]=169;
  3367. buf[x + y*stride-w]=64;
  3368. }
  3369. }
  3370. ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
  3371. #endif
  3372. for(y=0; y<height; y++){
  3373. for(x=0; x<width; x++){
  3374. int64_t d= buffer[0][x + y*width];
  3375. error += d*d;
  3376. if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
  3377. }
  3378. if(ABS(height/2-y)<9) printf("\n");
  3379. }
  3380. }
  3381. }
  3382. return 0;
  3383. }
  3384. #endif