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.

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