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.

4706 lines
168KB

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