rgb_2_rgb.asm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;******************************************************************************
  2. ;* Copyright Nick Kurshev
  3. ;* Copyright Michael (michaelni@gmx.at)
  4. ;* Copyright 2018 Jokyo Images
  5. ;* Copyright Ivo van Poorten
  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 "libavutil/x86/x86util.asm"
  24. SECTION_RODATA
  25. pb_shuffle2103: db 2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15
  26. pb_shuffle0321: db 0, 3, 2, 1, 4, 7, 6, 5, 8, 11, 10, 9, 12, 15, 14, 13
  27. pb_shuffle1230: db 1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12
  28. pb_shuffle3012: db 3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14
  29. pb_shuffle3210: db 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12
  30. SECTION .text
  31. %macro RSHIFT_COPY 5
  32. ; %1 dst ; %2 src ; %3 shift
  33. %if mmsize == 32
  34. vperm2i128 %1, %2, %3, %5
  35. RSHIFT %1, %4
  36. %elif cpuflag(avx)
  37. psrldq %1, %2, %4
  38. %else
  39. mova %1, %2
  40. RSHIFT %1, %4
  41. %endif
  42. %endmacro
  43. ;------------------------------------------------------------------------------
  44. ; shuffle_bytes_## (const uint8_t *src, uint8_t *dst, int src_size)
  45. ;------------------------------------------------------------------------------
  46. ; %1-4 index shuffle
  47. %macro SHUFFLE_BYTES 4
  48. cglobal shuffle_bytes_%1%2%3%4, 3, 5, 2, src, dst, w, tmp, x
  49. VBROADCASTI128 m0, [pb_shuffle%1%2%3%4]
  50. movsxdifnidn wq, wd
  51. mov xq, wq
  52. add srcq, wq
  53. add dstq, wq
  54. neg wq
  55. ;calc scalar loop
  56. and xq, mmsize-4
  57. je .loop_simd
  58. .loop_scalar:
  59. mov tmpb, [srcq + wq + %1]
  60. mov [dstq+wq + 0], tmpb
  61. mov tmpb, [srcq + wq + %2]
  62. mov [dstq+wq + 1], tmpb
  63. mov tmpb, [srcq + wq + %3]
  64. mov [dstq+wq + 2], tmpb
  65. mov tmpb, [srcq + wq + %4]
  66. mov [dstq+wq + 3], tmpb
  67. add wq, 4
  68. sub xq, 4
  69. jg .loop_scalar
  70. ;check if src_size < mmsize
  71. cmp wq, 0
  72. jge .end
  73. .loop_simd:
  74. movu m1, [srcq+wq]
  75. pshufb m1, m0
  76. movu [dstq+wq], m1
  77. add wq, mmsize
  78. jl .loop_simd
  79. .end:
  80. RET
  81. %endmacro
  82. INIT_XMM ssse3
  83. SHUFFLE_BYTES 2, 1, 0, 3
  84. SHUFFLE_BYTES 0, 3, 2, 1
  85. SHUFFLE_BYTES 1, 2, 3, 0
  86. SHUFFLE_BYTES 3, 0, 1, 2
  87. SHUFFLE_BYTES 3, 2, 1, 0
  88. %if ARCH_X86_64
  89. %if HAVE_AVX2_EXTERNAL
  90. INIT_YMM avx2
  91. SHUFFLE_BYTES 2, 1, 0, 3
  92. SHUFFLE_BYTES 0, 3, 2, 1
  93. SHUFFLE_BYTES 1, 2, 3, 0
  94. SHUFFLE_BYTES 3, 0, 1, 2
  95. SHUFFLE_BYTES 3, 2, 1, 0
  96. %endif
  97. %endif
  98. ;-----------------------------------------------------------------------------------------------
  99. ; uyvytoyuv422(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  100. ; const uint8_t *src, int width, int height,
  101. ; int lumStride, int chromStride, int srcStride)
  102. ;-----------------------------------------------------------------------------------------------
  103. %macro UYVY_TO_YUV422 0
  104. cglobal uyvytoyuv422, 9, 14, 8, ydst, udst, vdst, src, w, h, lum_stride, chrom_stride, src_stride, wtwo, whalf, tmp, x, back_w
  105. pxor m0, m0
  106. pcmpeqw m1, m1
  107. psrlw m1, 8
  108. movsxdifnidn wq, wd
  109. movsxdifnidn lum_strideq, lum_strided
  110. movsxdifnidn chrom_strideq, chrom_strided
  111. movsxdifnidn src_strideq, src_strided
  112. mov back_wq, wq
  113. mov whalfq, wq
  114. shr whalfq, 1 ; whalf = width / 2
  115. lea srcq, [srcq + wq * 2]
  116. add ydstq, wq
  117. add udstq, whalfq
  118. add vdstq, whalfq
  119. .loop_line:
  120. mov xq, wq
  121. mov wtwoq, wq
  122. add wtwoq, wtwoq ; wtwo = width * 2
  123. neg wq
  124. neg wtwoq
  125. neg whalfq
  126. ;calc scalar loop count
  127. and xq, mmsize * 2 - 1
  128. je .loop_simd
  129. .loop_scalar:
  130. mov tmpb, [srcq + wtwoq + 0]
  131. mov [udstq + whalfq], tmpb
  132. mov tmpb, [srcq + wtwoq + 1]
  133. mov [ydstq + wq], tmpb
  134. mov tmpb, [srcq + wtwoq + 2]
  135. mov [vdstq + whalfq], tmpb
  136. mov tmpb, [srcq + wtwoq + 3]
  137. mov [ydstq + wq + 1], tmpb
  138. add wq, 2
  139. add wtwoq, 4
  140. add whalfq, 1
  141. sub xq, 2
  142. jg .loop_scalar
  143. ; check if simd loop is need
  144. cmp wq, 0
  145. jge .end_line
  146. .loop_simd:
  147. %if mmsize == 32
  148. movu xm2, [srcq + wtwoq ]
  149. movu xm3, [srcq + wtwoq + 16 ]
  150. movu xm4, [srcq + wtwoq + 16 * 2]
  151. movu xm5, [srcq + wtwoq + 16 * 3]
  152. vinserti128 m2, m2, [srcq + wtwoq + 16 * 4], 1
  153. vinserti128 m3, m3, [srcq + wtwoq + 16 * 5], 1
  154. vinserti128 m4, m4, [srcq + wtwoq + 16 * 6], 1
  155. vinserti128 m5, m5, [srcq + wtwoq + 16 * 7], 1
  156. %else
  157. movu m2, [srcq + wtwoq ]
  158. movu m3, [srcq + wtwoq + mmsize ]
  159. movu m4, [srcq + wtwoq + mmsize * 2]
  160. movu m5, [srcq + wtwoq + mmsize * 3]
  161. %endif
  162. ; extract y part 1
  163. RSHIFT_COPY m6, m2, m4, 1, 0x20 ; UYVY UYVY -> YVYU YVY...
  164. pand m6, m1; YxYx YxYx...
  165. RSHIFT_COPY m7, m3, m5, 1, 0x20 ; UYVY UYVY -> YVYU YVY...
  166. pand m7, m1 ; YxYx YxYx...
  167. packuswb m6, m7 ; YYYY YYYY...
  168. movu [ydstq + wq], m6
  169. ; extract y part 2
  170. RSHIFT_COPY m6, m4, m2, 1, 0x13 ; UYVY UYVY -> YVYU YVY...
  171. pand m6, m1; YxYx YxYx...
  172. RSHIFT_COPY m7, m5, m3, 1, 0x13 ; UYVY UYVY -> YVYU YVY...
  173. pand m7, m1 ; YxYx YxYx...
  174. packuswb m6, m7 ; YYYY YYYY...
  175. movu [ydstq + wq + mmsize], m6
  176. ; extract uv
  177. pand m2, m1 ; UxVx...
  178. pand m3, m1 ; UxVx...
  179. pand m4, m1 ; UxVx...
  180. pand m5, m1 ; UxVx...
  181. packuswb m2, m3 ; UVUV...
  182. packuswb m4, m5 ; UVUV...
  183. ; U
  184. pand m6, m2, m1 ; UxUx...
  185. pand m7, m4, m1 ; UxUx...
  186. packuswb m6, m7 ; UUUU
  187. movu [udstq + whalfq], m6
  188. ; V
  189. psrlw m2, 8 ; VxVx...
  190. psrlw m4, 8 ; VxVx...
  191. packuswb m2, m4 ; VVVV
  192. movu [vdstq + whalfq], m2
  193. add whalfq, mmsize
  194. add wtwoq, mmsize * 4
  195. add wq, mmsize * 2
  196. jl .loop_simd
  197. .end_line:
  198. add srcq, src_strideq
  199. add ydstq, lum_strideq
  200. add udstq, chrom_strideq
  201. add vdstq, chrom_strideq
  202. ;restore initial state of line variable
  203. mov wq, back_wq
  204. mov xq, wq
  205. mov whalfq, wq
  206. shr whalfq, 1 ; whalf = width / 2
  207. sub hd, 1
  208. jg .loop_line
  209. RET
  210. %endmacro
  211. %if ARCH_X86_64
  212. INIT_XMM sse2
  213. UYVY_TO_YUV422
  214. INIT_XMM avx
  215. UYVY_TO_YUV422
  216. %if HAVE_AVX2_EXTERNAL
  217. INIT_YMM avx2
  218. UYVY_TO_YUV422
  219. %endif
  220. %endif