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.

3901 lines
132KB

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