yuv2yuv_altivec.c 8.4 KB

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