colorspacedsp_yuv2yuv_template.c 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
  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 "libavutil/avassert.h"
  21. #undef opixel
  22. #define opixel pixel
  23. #undef ipixel
  24. #if IN_BIT_DEPTH == 8
  25. #define ipixel uint8_t
  26. #else
  27. #define ipixel uint16_t
  28. #endif
  29. #undef fn
  30. #undef fn2
  31. #undef fn3
  32. #define fn3(a,b,c,d) a##_##d##p##b##to##c##_c
  33. #define fn2(a,b,c,d) fn3(a,b,c,d)
  34. #define fn(a) fn2(a, IN_BIT_DEPTH, OUT_BIT_DEPTH, ss)
  35. static void fn(yuv2yuv)(uint8_t *_dst[3], const ptrdiff_t dst_stride[3],
  36. uint8_t *_src[3], const ptrdiff_t src_stride[3],
  37. int w, int h, const int16_t c[3][3][8],
  38. const int16_t yuv_offset[2][8])
  39. {
  40. opixel **dst = (opixel **) _dst;
  41. ipixel **src = (ipixel **) _src;
  42. const ipixel *src0 = src[0], *src1 = src[1], *src2 = src[2];
  43. opixel *dst0 = dst[0], *dst1 = dst[1], *dst2 = dst[2];
  44. int y, x;
  45. const int sh = 14 + IN_BIT_DEPTH - OUT_BIT_DEPTH;
  46. const int rnd = 1 << (sh - 1);
  47. int y_off_in = yuv_offset[0][0];
  48. int y_off_out = yuv_offset[1][0] << sh;
  49. const int uv_off_in = 128 << (IN_BIT_DEPTH - 8);
  50. const int uv_off_out = rnd + (128 << (OUT_BIT_DEPTH - 8 + sh));
  51. int cyy = c[0][0][0], cyu = c[0][1][0], cyv = c[0][2][0];
  52. int cuu = c[1][1][0], cuv = c[1][2][0], cvu = c[2][1][0], cvv = c[2][2][0];
  53. av_assert2(c[1][0][0] == 0);
  54. av_assert2(c[2][0][0] == 0);
  55. w = AV_CEIL_RSHIFT(w, SS_W);
  56. h = AV_CEIL_RSHIFT(h, SS_H);
  57. for (y = 0; y < h; y++) {
  58. for (x = 0; x < w; x++) {
  59. int y00 = src0[x << SS_W] - y_off_in;
  60. #if SS_W == 1
  61. int y01 = src0[2 * x + 1] - y_off_in;
  62. #if SS_H == 1
  63. int y10 = src0[src_stride[0] / sizeof(ipixel) + 2 * x] - y_off_in;
  64. int y11 = src0[src_stride[0] / sizeof(ipixel) + 2 * x + 1] - y_off_in;
  65. #endif
  66. #endif
  67. int u = src1[x] - uv_off_in, v = src2[x] - uv_off_in;
  68. int uv_val = cyu * u + cyv * v + rnd + y_off_out;
  69. dst0[x << SS_W] = av_clip_pixel((cyy * y00 + uv_val) >> sh);
  70. #if SS_W == 1
  71. dst0[x * 2 + 1] = av_clip_pixel((cyy * y01 + uv_val) >> sh);
  72. #if SS_H == 1
  73. dst0[x * 2 + 0 + dst_stride[0] / sizeof(opixel)] =
  74. av_clip_pixel((cyy * y10 + uv_val) >> sh);
  75. dst0[x * 2 + 1 + dst_stride[0] / sizeof(opixel)] =
  76. av_clip_pixel((cyy * y11 + uv_val) >> sh);
  77. #endif
  78. #endif
  79. dst1[x] = av_clip_pixel((u * cuu + v * cuv + uv_off_out) >> sh);
  80. dst2[x] = av_clip_pixel((u * cvu + v * cvv + uv_off_out) >> sh);
  81. }
  82. dst0 += (dst_stride[0] * (1 << SS_H)) / sizeof(opixel);
  83. dst1 += dst_stride[1] / sizeof(opixel);
  84. dst2 += dst_stride[2] / sizeof(opixel);
  85. src0 += (src_stride[0] * (1 << SS_H)) / sizeof(ipixel);
  86. src1 += src_stride[1] / sizeof(ipixel);
  87. src2 += src_stride[2] / sizeof(ipixel);
  88. }
  89. }