yuv2yuv_altivec.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * AltiVec-enhanced yuv-to-yuv conversion routines.
  3. *
  4. * Copyright (C) 2004 Romain Dolbeau <romain@dolbeau.org>
  5. * based on the equivalent C code in swscale.c
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <inttypes.h>
  24. #include "config.h"
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/cpu.h"
  27. #include "libswscale/swscale.h"
  28. #include "libswscale/swscale_internal.h"
  29. #if HAVE_ALTIVEC
  30. static int yv12toyuy2_unscaled_altivec(SwsContext *c, const uint8_t *src[],
  31. int srcStride[], int srcSliceY,
  32. int srcSliceH, uint8_t *dstParam[],
  33. int dstStride_a[])
  34. {
  35. uint8_t *dst = dstParam[0] + dstStride_a[0] * srcSliceY;
  36. // yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH,
  37. // srcStride[0], srcStride[1], dstStride[0]);
  38. const uint8_t *ysrc = src[0];
  39. const uint8_t *usrc = src[1];
  40. const uint8_t *vsrc = src[2];
  41. const int width = c->srcW;
  42. const int height = srcSliceH;
  43. const int lumStride = srcStride[0];
  44. const int chromStride = srcStride[1];
  45. const int dstStride = dstStride_a[0];
  46. const vector unsigned char yperm = vec_lvsl(0, ysrc);
  47. const int vertLumPerChroma = 2;
  48. register unsigned int y;
  49. /* This code assumes:
  50. *
  51. * 1) dst is 16 bytes-aligned
  52. * 2) dstStride is a multiple of 16
  53. * 3) width is a multiple of 16
  54. * 4) lum & chrom stride are multiples of 8
  55. */
  56. for (y = 0; y < height; y++) {
  57. int i;
  58. for (i = 0; i < width - 31; i += 32) {
  59. const unsigned int j = i >> 1;
  60. vector unsigned char v_yA = vec_ld(i, ysrc);
  61. vector unsigned char v_yB = vec_ld(i + 16, ysrc);
  62. vector unsigned char v_yC = vec_ld(i + 32, ysrc);
  63. vector unsigned char v_y1 = vec_perm(v_yA, v_yB, yperm);
  64. vector unsigned char v_y2 = vec_perm(v_yB, v_yC, yperm);
  65. vector unsigned char v_uA = vec_ld(j, usrc);
  66. vector unsigned char v_uB = vec_ld(j + 16, usrc);
  67. vector unsigned char v_u = vec_perm(v_uA, v_uB, vec_lvsl(j, usrc));
  68. vector unsigned char v_vA = vec_ld(j, vsrc);
  69. vector unsigned char v_vB = vec_ld(j + 16, vsrc);
  70. vector unsigned char v_v = vec_perm(v_vA, v_vB, vec_lvsl(j, vsrc));
  71. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  72. vector unsigned char v_uv_b = vec_mergel(v_u, v_v);
  73. vector unsigned char v_yuy2_0 = vec_mergeh(v_y1, v_uv_a);
  74. vector unsigned char v_yuy2_1 = vec_mergel(v_y1, v_uv_a);
  75. vector unsigned char v_yuy2_2 = vec_mergeh(v_y2, v_uv_b);
  76. vector unsigned char v_yuy2_3 = vec_mergel(v_y2, v_uv_b);
  77. vec_st(v_yuy2_0, (i << 1), dst);
  78. vec_st(v_yuy2_1, (i << 1) + 16, dst);
  79. vec_st(v_yuy2_2, (i << 1) + 32, dst);
  80. vec_st(v_yuy2_3, (i << 1) + 48, dst);
  81. }
  82. if (i < width) {
  83. const unsigned int j = i >> 1;
  84. vector unsigned char v_y1 = vec_ld(i, ysrc);
  85. vector unsigned char v_u = vec_ld(j, usrc);
  86. vector unsigned char v_v = vec_ld(j, vsrc);
  87. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  88. vector unsigned char v_yuy2_0 = vec_mergeh(v_y1, v_uv_a);
  89. vector unsigned char v_yuy2_1 = vec_mergel(v_y1, v_uv_a);
  90. vec_st(v_yuy2_0, (i << 1), dst);
  91. vec_st(v_yuy2_1, (i << 1) + 16, dst);
  92. }
  93. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  94. usrc += chromStride;
  95. vsrc += chromStride;
  96. }
  97. ysrc += lumStride;
  98. dst += dstStride;
  99. }
  100. return srcSliceH;
  101. }
  102. static int yv12touyvy_unscaled_altivec(SwsContext *c, const uint8_t *src[],
  103. int srcStride[], int srcSliceY,
  104. int srcSliceH, uint8_t *dstParam[],
  105. int dstStride_a[])
  106. {
  107. uint8_t *dst = dstParam[0] + dstStride_a[0] * srcSliceY;
  108. // yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH,
  109. // srcStride[0], srcStride[1], dstStride[0]);
  110. const uint8_t *ysrc = src[0];
  111. const uint8_t *usrc = src[1];
  112. const uint8_t *vsrc = src[2];
  113. const int width = c->srcW;
  114. const int height = srcSliceH;
  115. const int lumStride = srcStride[0];
  116. const int chromStride = srcStride[1];
  117. const int dstStride = dstStride_a[0];
  118. const int vertLumPerChroma = 2;
  119. const vector unsigned char yperm = vec_lvsl(0, ysrc);
  120. register unsigned int y;
  121. /* This code assumes:
  122. *
  123. * 1) dst is 16 bytes-aligned
  124. * 2) dstStride is a multiple of 16
  125. * 3) width is a multiple of 16
  126. * 4) lum & chrom stride are multiples of 8
  127. */
  128. for (y = 0; y < height; y++) {
  129. int i;
  130. for (i = 0; i < width - 31; i += 32) {
  131. const unsigned int j = i >> 1;
  132. vector unsigned char v_yA = vec_ld(i, ysrc);
  133. vector unsigned char v_yB = vec_ld(i + 16, ysrc);
  134. vector unsigned char v_yC = vec_ld(i + 32, ysrc);
  135. vector unsigned char v_y1 = vec_perm(v_yA, v_yB, yperm);
  136. vector unsigned char v_y2 = vec_perm(v_yB, v_yC, yperm);
  137. vector unsigned char v_uA = vec_ld(j, usrc);
  138. vector unsigned char v_uB = vec_ld(j + 16, usrc);
  139. vector unsigned char v_u = vec_perm(v_uA, v_uB, vec_lvsl(j, usrc));
  140. vector unsigned char v_vA = vec_ld(j, vsrc);
  141. vector unsigned char v_vB = vec_ld(j + 16, vsrc);
  142. vector unsigned char v_v = vec_perm(v_vA, v_vB, vec_lvsl(j, vsrc));
  143. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  144. vector unsigned char v_uv_b = vec_mergel(v_u, v_v);
  145. vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);
  146. vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);
  147. vector unsigned char v_uyvy_2 = vec_mergeh(v_uv_b, v_y2);
  148. vector unsigned char v_uyvy_3 = vec_mergel(v_uv_b, v_y2);
  149. vec_st(v_uyvy_0, (i << 1), dst);
  150. vec_st(v_uyvy_1, (i << 1) + 16, dst);
  151. vec_st(v_uyvy_2, (i << 1) + 32, dst);
  152. vec_st(v_uyvy_3, (i << 1) + 48, dst);
  153. }
  154. if (i < width) {
  155. const unsigned int j = i >> 1;
  156. vector unsigned char v_y1 = vec_ld(i, ysrc);
  157. vector unsigned char v_u = vec_ld(j, usrc);
  158. vector unsigned char v_v = vec_ld(j, vsrc);
  159. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  160. vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);
  161. vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);
  162. vec_st(v_uyvy_0, (i << 1), dst);
  163. vec_st(v_uyvy_1, (i << 1) + 16, dst);
  164. }
  165. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  166. usrc += chromStride;
  167. vsrc += chromStride;
  168. }
  169. ysrc += lumStride;
  170. dst += dstStride;
  171. }
  172. return srcSliceH;
  173. }
  174. #endif /* HAVE_ALTIVEC */
  175. av_cold void ff_get_unscaled_swscale_ppc(SwsContext *c)
  176. {
  177. #if HAVE_ALTIVEC
  178. if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  179. return;
  180. if (!(c->srcW & 15) && !(c->flags & SWS_BITEXACT) &&
  181. c->srcFormat == AV_PIX_FMT_YUV420P) {
  182. enum AVPixelFormat dstFormat = c->dstFormat;
  183. // unscaled YV12 -> packed YUV, we want speed
  184. if (dstFormat == AV_PIX_FMT_YUYV422)
  185. c->convert_unscaled = yv12toyuy2_unscaled_altivec;
  186. else if (dstFormat == AV_PIX_FMT_UYVY422)
  187. c->convert_unscaled = yv12touyvy_unscaled_altivec;
  188. }
  189. #endif /* HAVE_ALTIVEC */
  190. }