yuv2yuv_altivec.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "libswscale/swscale.h"
  26. #include "libswscale/swscale_internal.h"
  27. #include "libavutil/cpu.h"
  28. #if HAVE_ALTIVEC
  29. static int yv12toyuy2_unscaled_altivec(SwsContext *c, const uint8_t *src[],
  30. int srcStride[], int srcSliceY,
  31. int srcSliceH, uint8_t *dstParam[],
  32. int dstStride_a[])
  33. {
  34. uint8_t *dst = dstParam[0] + dstStride_a[0] * srcSliceY;
  35. // yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH,
  36. // srcStride[0], srcStride[1], dstStride[0]);
  37. const uint8_t *ysrc = src[0];
  38. const uint8_t *usrc = src[1];
  39. const uint8_t *vsrc = src[2];
  40. const int width = c->srcW;
  41. const int height = srcSliceH;
  42. const int lumStride = srcStride[0];
  43. const int chromStride = srcStride[1];
  44. const int dstStride = dstStride_a[0];
  45. const vector unsigned char yperm = vec_lvsl(0, ysrc);
  46. const int vertLumPerChroma = 2;
  47. register unsigned int y;
  48. /* This code assumes:
  49. *
  50. * 1) dst is 16 bytes-aligned
  51. * 2) dstStride is a multiple of 16
  52. * 3) width is a multiple of 16
  53. * 4) lum & chrom stride are multiples of 8
  54. */
  55. for (y = 0; y < height; y++) {
  56. int i;
  57. for (i = 0; i < width - 31; i += 32) {
  58. const unsigned int j = i >> 1;
  59. vector unsigned char v_yA = vec_ld(i, ysrc);
  60. vector unsigned char v_yB = vec_ld(i + 16, ysrc);
  61. vector unsigned char v_yC = vec_ld(i + 32, ysrc);
  62. vector unsigned char v_y1 = vec_perm(v_yA, v_yB, yperm);
  63. vector unsigned char v_y2 = vec_perm(v_yB, v_yC, yperm);
  64. vector unsigned char v_uA = vec_ld(j, usrc);
  65. vector unsigned char v_uB = vec_ld(j + 16, usrc);
  66. vector unsigned char v_u = vec_perm(v_uA, v_uB, vec_lvsl(j, usrc));
  67. vector unsigned char v_vA = vec_ld(j, vsrc);
  68. vector unsigned char v_vB = vec_ld(j + 16, vsrc);
  69. vector unsigned char v_v = vec_perm(v_vA, v_vB, vec_lvsl(j, vsrc));
  70. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  71. vector unsigned char v_uv_b = vec_mergel(v_u, v_v);
  72. vector unsigned char v_yuy2_0 = vec_mergeh(v_y1, v_uv_a);
  73. vector unsigned char v_yuy2_1 = vec_mergel(v_y1, v_uv_a);
  74. vector unsigned char v_yuy2_2 = vec_mergeh(v_y2, v_uv_b);
  75. vector unsigned char v_yuy2_3 = vec_mergel(v_y2, v_uv_b);
  76. vec_st(v_yuy2_0, (i << 1), dst);
  77. vec_st(v_yuy2_1, (i << 1) + 16, dst);
  78. vec_st(v_yuy2_2, (i << 1) + 32, dst);
  79. vec_st(v_yuy2_3, (i << 1) + 48, dst);
  80. }
  81. if (i < width) {
  82. const unsigned int j = i >> 1;
  83. vector unsigned char v_y1 = vec_ld(i, ysrc);
  84. vector unsigned char v_u = vec_ld(j, usrc);
  85. vector unsigned char v_v = vec_ld(j, vsrc);
  86. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  87. vector unsigned char v_yuy2_0 = vec_mergeh(v_y1, v_uv_a);
  88. vector unsigned char v_yuy2_1 = vec_mergel(v_y1, v_uv_a);
  89. vec_st(v_yuy2_0, (i << 1), dst);
  90. vec_st(v_yuy2_1, (i << 1) + 16, dst);
  91. }
  92. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  93. usrc += chromStride;
  94. vsrc += chromStride;
  95. }
  96. ysrc += lumStride;
  97. dst += dstStride;
  98. }
  99. return srcSliceH;
  100. }
  101. static int yv12touyvy_unscaled_altivec(SwsContext *c, const uint8_t *src[],
  102. int srcStride[], int srcSliceY,
  103. int srcSliceH, uint8_t *dstParam[],
  104. int dstStride_a[])
  105. {
  106. uint8_t *dst = dstParam[0] + dstStride_a[0] * srcSliceY;
  107. // yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH,
  108. // srcStride[0], srcStride[1], dstStride[0]);
  109. const uint8_t *ysrc = src[0];
  110. const uint8_t *usrc = src[1];
  111. const uint8_t *vsrc = src[2];
  112. const int width = c->srcW;
  113. const int height = srcSliceH;
  114. const int lumStride = srcStride[0];
  115. const int chromStride = srcStride[1];
  116. const int dstStride = dstStride_a[0];
  117. const int vertLumPerChroma = 2;
  118. const vector unsigned char yperm = vec_lvsl(0, ysrc);
  119. register unsigned int y;
  120. /* This code assumes:
  121. *
  122. * 1) dst is 16 bytes-aligned
  123. * 2) dstStride is a multiple of 16
  124. * 3) width is a multiple of 16
  125. * 4) lum & chrom stride are multiples of 8
  126. */
  127. for (y = 0; y < height; y++) {
  128. int i;
  129. for (i = 0; i < width - 31; i += 32) {
  130. const unsigned int j = i >> 1;
  131. vector unsigned char v_yA = vec_ld(i, ysrc);
  132. vector unsigned char v_yB = vec_ld(i + 16, ysrc);
  133. vector unsigned char v_yC = vec_ld(i + 32, ysrc);
  134. vector unsigned char v_y1 = vec_perm(v_yA, v_yB, yperm);
  135. vector unsigned char v_y2 = vec_perm(v_yB, v_yC, yperm);
  136. vector unsigned char v_uA = vec_ld(j, usrc);
  137. vector unsigned char v_uB = vec_ld(j + 16, usrc);
  138. vector unsigned char v_u = vec_perm(v_uA, v_uB, vec_lvsl(j, usrc));
  139. vector unsigned char v_vA = vec_ld(j, vsrc);
  140. vector unsigned char v_vB = vec_ld(j + 16, vsrc);
  141. vector unsigned char v_v = vec_perm(v_vA, v_vB, vec_lvsl(j, vsrc));
  142. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  143. vector unsigned char v_uv_b = vec_mergel(v_u, v_v);
  144. vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);
  145. vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);
  146. vector unsigned char v_uyvy_2 = vec_mergeh(v_uv_b, v_y2);
  147. vector unsigned char v_uyvy_3 = vec_mergel(v_uv_b, v_y2);
  148. vec_st(v_uyvy_0, (i << 1), dst);
  149. vec_st(v_uyvy_1, (i << 1) + 16, dst);
  150. vec_st(v_uyvy_2, (i << 1) + 32, dst);
  151. vec_st(v_uyvy_3, (i << 1) + 48, dst);
  152. }
  153. if (i < width) {
  154. const unsigned int j = i >> 1;
  155. vector unsigned char v_y1 = vec_ld(i, ysrc);
  156. vector unsigned char v_u = vec_ld(j, usrc);
  157. vector unsigned char v_v = vec_ld(j, vsrc);
  158. vector unsigned char v_uv_a = vec_mergeh(v_u, v_v);
  159. vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);
  160. vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);
  161. vec_st(v_uyvy_0, (i << 1), dst);
  162. vec_st(v_uyvy_1, (i << 1) + 16, dst);
  163. }
  164. if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
  165. usrc += chromStride;
  166. vsrc += chromStride;
  167. }
  168. ysrc += lumStride;
  169. dst += dstStride;
  170. }
  171. return srcSliceH;
  172. }
  173. #endif /* HAVE_ALTIVEC */
  174. void ff_swscale_get_unscaled_ppc(SwsContext *c)
  175. {
  176. #if HAVE_ALTIVEC
  177. if ((av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) && !(c->srcW & 15) &&
  178. !(c->flags & SWS_BITEXACT) && c->srcFormat == AV_PIX_FMT_YUV420P) {
  179. enum AVPixelFormat dstFormat = c->dstFormat;
  180. // unscaled YV12 -> packed YUV, we want speed
  181. if (dstFormat == AV_PIX_FMT_YUYV422)
  182. c->swScale = yv12toyuy2_unscaled_altivec;
  183. else if (dstFormat == AV_PIX_FMT_UYVY422)
  184. c->swScale = yv12touyvy_unscaled_altivec;
  185. }
  186. #endif /* HAVE_ALTIVEC */
  187. }