rgb_2_rgb.asm 7.8 KB

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