yuv2yuv_altivec.c 7.9 KB

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