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.

4358 lines
148KB

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