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.

3903 lines
132KB

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