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.

282 lines
10KB

  1. /*
  2. * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
  3. * Copyright (C) 2016 Open Broadcast Systems Ltd.
  4. * Author 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/mem.h"
  24. #include "vc2enc_dwt.h"
  25. /* Since the transforms spit out interleaved coefficients, this function
  26. * rearranges the coefficients into the more traditional subdivision,
  27. * making it easier to encode and perform another level. */
  28. static av_always_inline void deinterleave(dwtcoef *linell, ptrdiff_t stride,
  29. int width, int height, dwtcoef *synthl)
  30. {
  31. int x, y;
  32. ptrdiff_t synthw = width << 1;
  33. dwtcoef *linehl = linell + width;
  34. dwtcoef *linelh = linell + height*stride;
  35. dwtcoef *linehh = linelh + width;
  36. /* Deinterleave the coefficients. */
  37. for (y = 0; y < height; y++) {
  38. for (x = 0; x < width; x++) {
  39. linell[x] = synthl[(x << 1)];
  40. linehl[x] = synthl[(x << 1) + 1];
  41. linelh[x] = synthl[(x << 1) + synthw];
  42. linehh[x] = synthl[(x << 1) + synthw + 1];
  43. }
  44. synthl += synthw << 1;
  45. linell += stride;
  46. linelh += stride;
  47. linehl += stride;
  48. linehh += stride;
  49. }
  50. }
  51. static void vc2_subband_dwt_97(VC2TransformContext *t, dwtcoef *data,
  52. ptrdiff_t stride, int width, int height)
  53. {
  54. int x, y;
  55. dwtcoef *datal = data, *synth = t->buffer, *synthl = synth;
  56. const ptrdiff_t synth_width = width << 1;
  57. const ptrdiff_t synth_height = height << 1;
  58. /*
  59. * Shift in one bit that is used for additional precision and copy
  60. * the data to the buffer.
  61. */
  62. for (y = 0; y < synth_height; y++) {
  63. for (x = 0; x < synth_width; x++)
  64. synthl[x] = datal[x] << 1;
  65. synthl += synth_width;
  66. datal += stride;
  67. }
  68. /* Horizontal synthesis. */
  69. synthl = synth;
  70. for (y = 0; y < synth_height; y++) {
  71. /* Lifting stage 2. */
  72. synthl[1] -= (8*synthl[0] + 9*synthl[2] - synthl[4] + 8) >> 4;
  73. for (x = 1; x < width - 2; x++)
  74. synthl[2*x + 1] -= (9*synthl[2*x] + 9*synthl[2*x + 2] - synthl[2*x + 4] -
  75. synthl[2 * x - 2] + 8) >> 4;
  76. synthl[synth_width - 1] -= (17*synthl[synth_width - 2] -
  77. synthl[synth_width - 4] + 8) >> 4;
  78. synthl[synth_width - 3] -= (8*synthl[synth_width - 2] +
  79. 9*synthl[synth_width - 4] -
  80. synthl[synth_width - 6] + 8) >> 4;
  81. /* Lifting stage 1. */
  82. synthl[0] += (synthl[1] + synthl[1] + 2) >> 2;
  83. for (x = 1; x < width - 1; x++)
  84. synthl[2*x] += (synthl[2*x - 1] + synthl[2*x + 1] + 2) >> 2;
  85. synthl[synth_width - 2] += (synthl[synth_width - 3] +
  86. synthl[synth_width - 1] + 2) >> 2;
  87. synthl += synth_width;
  88. }
  89. /* Vertical synthesis: Lifting stage 2. */
  90. synthl = synth + synth_width;
  91. for (x = 0; x < synth_width; x++)
  92. synthl[x] -= (8*synthl[x - synth_width] + 9*synthl[x + synth_width] -
  93. synthl[x + 3 * synth_width] + 8) >> 4;
  94. synthl = synth + (synth_width << 1);
  95. for (y = 1; y < height - 2; y++) {
  96. for (x = 0; x < synth_width; x++)
  97. synthl[x + synth_width] -= (9*synthl[x] +
  98. 9*synthl[x + 2 * synth_width] -
  99. synthl[x - 2 * synth_width] -
  100. synthl[x + 4 * synth_width] + 8) >> 4;
  101. synthl += synth_width << 1;
  102. }
  103. synthl = synth + (synth_height - 1) * synth_width;
  104. for (x = 0; x < synth_width; x++) {
  105. synthl[x] -= (17*synthl[x - synth_width] -
  106. synthl[x - 3*synth_width] + 8) >> 4;
  107. synthl[x - 2*synth_width] -= (9*synthl[x - 3*synth_width] +
  108. 8*synthl[x - 1*synth_width] - synthl[x - 5*synth_width] + 8) >> 4;
  109. }
  110. /* Vertical synthesis: Lifting stage 1. */
  111. synthl = synth;
  112. for (x = 0; x < synth_width; x++)
  113. synthl[x] += (synthl[x + synth_width] + synthl[x + synth_width] + 2) >> 2;
  114. synthl = synth + (synth_width << 1);
  115. for (y = 1; y < height - 1; y++) {
  116. for (x = 0; x < synth_width; x++)
  117. synthl[x] += (synthl[x - synth_width] + synthl[x + synth_width] + 2) >> 2;
  118. synthl += synth_width << 1;
  119. }
  120. synthl = synth + (synth_height - 2) * synth_width;
  121. for (x = 0; x < synth_width; x++)
  122. synthl[x] += (synthl[x - synth_width] + synthl[x + synth_width] + 2) >> 2;
  123. deinterleave(data, stride, width, height, synth);
  124. }
  125. static void vc2_subband_dwt_53(VC2TransformContext *t, dwtcoef *data,
  126. ptrdiff_t stride, int width, int height)
  127. {
  128. int x, y;
  129. dwtcoef *synth = t->buffer, *synthl = synth, *datal = data;
  130. const ptrdiff_t synth_width = width << 1;
  131. const ptrdiff_t synth_height = height << 1;
  132. /*
  133. * Shift in one bit that is used for additional precision and copy
  134. * the data to the buffer.
  135. */
  136. for (y = 0; y < synth_height; y++) {
  137. for (x = 0; x < synth_width; x++)
  138. synthl[x] = datal[x] << 1;
  139. synthl += synth_width;
  140. datal += stride;
  141. }
  142. /* Horizontal synthesis. */
  143. synthl = synth;
  144. for (y = 0; y < synth_height; y++) {
  145. /* Lifting stage 2. */
  146. for (x = 0; x < width - 1; x++)
  147. synthl[2 * x + 1] -= (synthl[2 * x] + synthl[2 * x + 2] + 1) >> 1;
  148. synthl[synth_width - 1] -= (2*synthl[synth_width - 2] + 1) >> 1;
  149. /* Lifting stage 1. */
  150. synthl[0] += (2*synthl[1] + 2) >> 2;
  151. for (x = 1; x < width - 1; x++)
  152. synthl[2 * x] += (synthl[2 * x - 1] + synthl[2 * x + 1] + 2) >> 2;
  153. synthl[synth_width - 2] += (synthl[synth_width - 3] + synthl[synth_width - 1] + 2) >> 2;
  154. synthl += synth_width;
  155. }
  156. /* Vertical synthesis: Lifting stage 2. */
  157. synthl = synth + synth_width;
  158. for (x = 0; x < synth_width; x++)
  159. synthl[x] -= (synthl[x - synth_width] + synthl[x + synth_width] + 1) >> 1;
  160. synthl = synth + (synth_width << 1);
  161. for (y = 1; y < height - 1; y++) {
  162. for (x = 0; x < synth_width; x++)
  163. synthl[x + synth_width] -= (synthl[x] + synthl[x + synth_width * 2] + 1) >> 1;
  164. synthl += (synth_width << 1);
  165. }
  166. synthl = synth + (synth_height - 1) * synth_width;
  167. for (x = 0; x < synth_width; x++)
  168. synthl[x] -= (2*synthl[x - synth_width] + 1) >> 1;
  169. /* Vertical synthesis: Lifting stage 1. */
  170. synthl = synth;
  171. for (x = 0; x < synth_width; x++)
  172. synthl[x] += (2*synthl[synth_width + x] + 2) >> 2;
  173. synthl = synth + (synth_width << 1);
  174. for (y = 1; y < height - 1; y++) {
  175. for (x = 0; x < synth_width; x++)
  176. synthl[x] += (synthl[x + synth_width] + synthl[x - synth_width] + 2) >> 2;
  177. synthl += (synth_width << 1);
  178. }
  179. synthl = synth + (synth_height - 2)*synth_width;
  180. for (x = 0; x < synth_width; x++)
  181. synthl[x] += (synthl[x - synth_width] + synthl[x + synth_width] + 2) >> 2;
  182. deinterleave(data, stride, width, height, synth);
  183. }
  184. static av_always_inline void dwt_haar(VC2TransformContext *t, dwtcoef *data,
  185. ptrdiff_t stride, int width, int height,
  186. const int s)
  187. {
  188. int x, y;
  189. dwtcoef *synth = t->buffer, *synthl = synth, *datal = data;
  190. const ptrdiff_t synth_width = width << 1;
  191. const ptrdiff_t synth_height = height << 1;
  192. /* Horizontal synthesis. */
  193. for (y = 0; y < synth_height; y++) {
  194. for (x = 0; x < synth_width; x += 2) {
  195. synthl[y*synth_width + x + 1] = (datal[y*stride + x + 1] << s) -
  196. (datal[y*stride + x] << s);
  197. synthl[y*synth_width + x] = (datal[y*stride + x + 0] << s) +
  198. ((synthl[y*synth_width + x + 1] + 1) >> 1);
  199. }
  200. }
  201. /* Vertical synthesis. */
  202. for (x = 0; x < synth_width; x++) {
  203. for (y = 0; y < synth_height; y += 2) {
  204. synthl[(y + 1)*synth_width + x] = synthl[(y + 1)*synth_width + x] -
  205. synthl[y*synth_width + x];
  206. synthl[y*synth_width + x] = synthl[y*synth_width + x] +
  207. ((synthl[(y + 1)*synth_width + x] + 1) >> 1);
  208. }
  209. }
  210. deinterleave(data, stride, width, height, synth);
  211. }
  212. static void vc2_subband_dwt_haar(VC2TransformContext *t, dwtcoef *data,
  213. ptrdiff_t stride, int width, int height)
  214. {
  215. dwt_haar(t, data, stride, width, height, 0);
  216. }
  217. static void vc2_subband_dwt_haar_shift(VC2TransformContext *t, dwtcoef *data,
  218. ptrdiff_t stride, int width, int height)
  219. {
  220. dwt_haar(t, data, stride, width, height, 1);
  221. }
  222. av_cold int ff_vc2enc_init_transforms(VC2TransformContext *s, int p_stride,
  223. int p_height, int slice_w, int slice_h)
  224. {
  225. s->vc2_subband_dwt[VC2_TRANSFORM_9_7] = vc2_subband_dwt_97;
  226. s->vc2_subband_dwt[VC2_TRANSFORM_5_3] = vc2_subband_dwt_53;
  227. s->vc2_subband_dwt[VC2_TRANSFORM_HAAR] = vc2_subband_dwt_haar;
  228. s->vc2_subband_dwt[VC2_TRANSFORM_HAAR_S] = vc2_subband_dwt_haar_shift;
  229. /* Pad by the slice size, only matters for non-Haar wavelets */
  230. s->buffer = av_calloc((p_stride + slice_w)*(p_height + slice_h), sizeof(dwtcoef));
  231. if (!s->buffer)
  232. return 1;
  233. s->padding = (slice_h >> 1)*p_stride + (slice_w >> 1);
  234. s->buffer += s->padding;
  235. return 0;
  236. }
  237. av_cold void ff_vc2enc_free_transforms(VC2TransformContext *s)
  238. {
  239. av_free(s->buffer - s->padding);
  240. s->buffer = NULL;
  241. }